Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit HyperlinkButton not downloading csv text file from server location to clent in browser window.
5 replies. Latest Post by hazz on July 3, 2009.
(0)
hazz
Member
74 points
258 Posts
07-02-2009 1:12 PM |
Given the HyperlinkButton xaml, the code behind for the button click, the service level creation of the csv/xls file, why is the browser not launching any "Save as' file dialog box or prompt when I click on the hyperlink? The .csv file is created in the directory at the root of the web project. Whether I hard code the actual path in the NavigateUri property to try a relative location as below,... neither works. Thank you for any help.
<HyperlinkButton Width="100" NavigateUri="../testfile.csv" Content="Save To Excel" TargetName="_parent" Click="Button_Click"></HyperlinkButton>
and
private void Button_Click(object sender, RoutedEventArgs e) { if (HtmlPage.Window.Confirm("Save report contents to Excel?")) { proxy.ExportDataTableToCSVCompleted += new EventHandler (proxy_ExportDataTableToCSVCompleted); proxy.ExportDataTableToCSVAsync();
private void exportDataTableToCsv(DataTable formattedDataTable, string filename) { StringBuilder sb = new StringBuilder(); foreach (DataColumn column in toExcel.Columns) { sb.Append(column.ColumnName + ","); } sb.Append(Environment.NewLine); foreach (DataRow row in toExcel.Rows) { for (int i = 0; i < toExcel.Columns.Count; i++) { sb.Append(row.ToString().Replace(",", string.Empty) + ","); } sb.Append(Environment.NewLine); } WriteFile(sb); }
void WriteFile(StringBuilder stringCSV) { string FileLocation = "C:\\.....\\.....\\....\\trunk"; string FileName = "testfile.csv"; FileStream fileStream = null; StreamWriter writer = null; fileStream = new FileStream(FileLocation + FileName, FileMode.OpenOrCreate, FileAccess.Write); writer = new StreamWriter(fileStream); writer.WriteLine(stringCSV.ToString()); writer.Flush(); }
jay nana...
Contributor
3388 points
624 Posts
07-02-2009 1:55 PM |
Remove Click event handler and try again.
07-02-2009 5:21 PM |
Thank you Jay! That did it!
Follow up question.
My intention was to have a button on the same page as the report/datagrid which
1. calls a service to create the file on the server and then
2. navigates to the uri of that newly created file via ( NavigateUri=http://localhost:1329/xxxx/testfile.csv ) .
How can I accomplish that if I can't call the click event which calls the service which creates the file AND then navigate to that uri?
I can see I was expecting too much from the Hyperlinkbutton.......but how do I accomplish both steps with one button?
Thank you,
07-03-2009 2:35 AM |
ok hazz, I am preparing sample for you and will post here.
07-03-2009 4:59 AM |
Hey, download the example from here:
http://cid-b55690d11b67401d.skydrive.live.com/embedrow.aspx/Public/FileCreate.zip
07-03-2009 8:37 AM |
Sweet.
Very nice.
With much appreciation.