Skip to main content

Microsoft Silverlight

Answered Question button on xaml launches server side launch of csv file download.RSS Feed

(0)

hazz
hazz

Member

Member

75 points

259 Posts

button on xaml launches server side launch of csv file download.

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(rowIdea.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();
 

hazz

IT_Lackey
IT_Lackey

Member

Member

519 points

117 Posts

Re: button on xaml launches server side launch of csv file download.

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...

If this was helpful, please click Mark as Answer so others can find threads that have solutions. Thanks!

hazz
hazz

Member

Member

75 points

259 Posts

Re: button on xaml launches server side launch of csv file download.

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();
                 }
            }

 

hazz

IT_Lackey
IT_Lackey

Member

Member

519 points

117 Posts

Answered Question

Re: Re: button on xaml launches server side launch of csv file download.

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.

If this was helpful, please click Mark as Answer so others can find threads that have solutions. Thanks!

hazz
hazz

Member

Member

75 points

259 Posts

Re: Re: button on xaml launches server side launch of csv file download.

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.

hazz

IT_Lackey
IT_Lackey

Member

Member

519 points

117 Posts

Re: Re: Re: button on xaml launches server side launch of csv file download.

Sounds good, if you get stuck let me know and I will see if I can whip up an example.

If this was helpful, please click Mark as Answer so others can find threads that have solutions. Thanks!

hazz
hazz

Member

Member

75 points

259 Posts

Re: Re: Re: button on xaml launches server side launch of csv file download.

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 !

hazz

IT_Lackey
IT_Lackey

Member

Member

519 points

117 Posts

Re: Re: Re: button on xaml launches server side launch of csv file download.

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.

If this was helpful, please click Mark as Answer so others can find threads that have solutions. Thanks!
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities