Skip to main content
Home Forums Silverlight Programming Programming with .NET - General button on xaml launches server side launch of csv file download.
7 replies. Latest Post by IT_Lackey on June 30, 2009.
(0)
hazz
Member
75 points
259 Posts
06-23-2009 3:09 PM |
given the following sequence of client side call to a server side csv file creation, what do I have to do in the way of a web control or container to accomodate the HttpContext.Current.ApplicationInstance.CompleteRequest(); statement at the bottom of this post. I would expect a dialog box appear on the "web container" to launch a dialog box to save the file just created to the client's machine. Thank you !
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();
WILL CALL THE FOLLOWING SERVER SIDE METHOD
#region "Export DataTable To CSV" [OperationContract] public void ExportDataTableToCSV() { DataTable dt = new DataTable(); string strSQL = "---sql statement -----"; SqlCommand command = new SqlCommand(strSQL, conn); SqlDataAdapter da = new SqlDataAdapter(command); string filename = "test.csv"; exportDataTableToCsv(dt, filename);
WHICH CALLS ANOTHER SERVER SIDE METHOD
private void exportDataTableToCsv(DataTable formattedDataTable, string filename) { DataTable toExcel = formattedDataTable.Copy(); HttpContext context = HttpContext.Current; context.Response.Clear(); foreach (DataColumn column in toExcel.Columns) { context.Response.Write(column.ColumnName + ","); } context.Response.Write(Environment.NewLine); foreach (DataRow row in toExcel.Rows) { for (int i = 0; i < toExcel.Columns.Count; i++) { context.Response.Write(row.ToString().Replace(",", string.Empty) + ","); } context.Response.Write(Environment.NewLine); } context.Response.ContentType = "text/csv"; context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv"); HttpContext.Current.ApplicationInstance.CompleteRequest();
IT_Lackey
519 points
117 Posts
06-23-2009 11:21 PM |
It would be much easier to save the file to the server and then display a link to download the newly created file that the user can then click on. You could launch the download in a new window if you wanted.
I am not sure sending a response will work since silverlight is actually running in a seperate context...
06-24-2009 8:07 AM |
Thank you !
How would I display a link to download the newly created file?
Or launch in new window given I just took your recommendation on the save file to server thought as pe the following. Thanks!
void WriteFile(StringBuilder stringCSV) { string FileLocation = "C:\\"; string FileName = "testfile.csv"; FileStream fileStream = null; StreamWriter writer = null; try { fileStream = new FileStream(FileLocation + FileName, FileMode.OpenOrCreate, FileAccess.Write); writer = new StreamWriter(fileStream); // Force the write to the underlying file writer.WriteLine(stringCSV.ToString()); writer.Flush(); } finally { if( writer != null ) writer.Close(); } }
06-24-2009 9:17 AM |
You would need to write the csv into the web folder that is hosting the silverlight application and then use the hyperlink control in silverlight to point to the new file. The webservice could return the url of the file it wrote. For example:
Silverlight is hosted at yoursite.com/default.aspx, you could then write the file to yoursite.com/csvfiles/newfile.csv and return that url from the webservice call. Then set the url of the hyperlink control to the returned url and have the target of the control set to _blank.
06-24-2009 12:50 PM |
Thank you. That sounds like a clear set of instructions. I'll report back as soon as i have a chance to code and test.
06-24-2009 1:10 PM |
Sounds good, if you get stuck let me know and I will see if I can whip up an example.
06-29-2009 8:14 AM |
I marked your idea as the answer..... but I haven't implemented yet...... is the purpose of the _blank to launch a popup for the user to save the file whose url has been returned from the service? Thanks for your patience !
06-30-2009 4:47 PM |
Yes the _blank will launch a new window. I am not 100% sure this is needed, but was trying to avoid the application refreshing and loosing any state that might be there. You can try it without the _blank target to see if it refreshes or not.