30

I hope this is a quick question I hope. I need to write some reports and then have the user prompted to save it to his/her local machine. The last time I did this I wrote a file to the webserver and then sent it to the client via Response object.

to create on the webserver

            TextWriter tw = new StreamWriter(filePath);

to send to client

           page.Response.WriteFile(path);

The question is, Is there a way to skip the writing of the physical file to to the webserver and go right from an object that represent the document to the response?

jim
  • 26,598
  • 13
  • 51
  • 66

4 Answers4

28

You could use the Response.ContentType like this

Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.txt");

This of course works if you want to write a text file. In case you want to write a .doc for example you change the ContentType to "application/msword" etc...

Mhd. Yasseen
  • 1,027
  • 3
  • 16
  • 27
Nikos Steiakakis
  • 5,605
  • 7
  • 44
  • 54
  • this code writes write to my browser. I get no prompt. IReport rpt = new ContxReport(); string report = rpt.makeReport(); ASCIIEncoding encoding = new ASCIIEncoding(); byte[] encodedReport = encoding.GetBytes(report); Response.ContentType = "text/plain"; Response.OutputStream.Write(encodedReport, 0, encodedReport.Length); Response.AddHeader("Content-Disposition", "inline;filename=shit.csv"); Response.End(); – jim Jul 02 '09 at 07:14
  • 1
    Also, you should set all headers before writing. – Talljoe Jul 02 '09 at 07:28
  • 2
    What is ***buffer*** ? – Kiquenet Oct 15 '15 at 14:15
  • 1
    buffer in this particular example is just a variable name for a byte array, byte[] buffer; Perhaps not the best of names as it might cause confusion, however back in 2009 when I wrote this answer I think I copy/pasted it from actual code. – Nikos Steiakakis Oct 15 '15 at 17:03
17

You can.

Try this:

Table oTable = new Table();
//Add data to table.

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename="test.xls"");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.Html32TextWriter oHtmlTextWriter = new System.Web.UI.Html32TextWriter(oStringWriter);
0Table.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();

This will give prompt the user to open or save test.xls file. similarly you can provide other ASP.NET objects in place of Table.

Ganesh R.
  • 4,337
  • 3
  • 30
  • 46
  • You should be able to use Response.Output instead of creating a StringWriter. – Talljoe Jul 02 '09 at 07:26
  • I have 2 questions. 1) Will that file resume capable.? and 2) Are multiple connection possible if using a download manager like IDM etc..? – shashwat Mar 02 '13 at 18:57
  • 2
    @ShashwatTripathi I am not sure. This was a piece of code I used long back. IMHO, it won't support resume or multiple download connections. – Ganesh R. Mar 02 '13 at 19:05
5

Yes.

page.Response.WriteFile(yourData, 0, yourData.Length);
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • 1
    I have 2 questions. 1) Will that file resume capable.? and 2) Are multiple connection possible if using a download manager like IDM etc..? – shashwat Mar 02 '13 at 18:58
5

The exact answer to the question depends on how do you have your report organized (i.e. what is the "object" you're referring to). If you have a single string, you can use Response.Write to just write it. If you need a TextWriter, Response.Output is what you want - that way you can skip writing to the disk and then using WriteFile. Also, if your content happened to be binary, you could use Response.OutputStream for that.

You may also want to set Response.AddHeader("Content-Disposition", "attachment"), if you want the user to see a save file dialog. However, Content-Disposition is not necessarily honored, so the user may still get the report opened directly in the browser. Remember to set headers before you output the actual content!

Also, depending on the format of the report, you may want to set Response.ContentType to something appropriate such as text/plain, text/csv or whatever have you. The correct mime types are listed at the IANA site.

Jouni Heikniemi
  • 2,244
  • 1
  • 18
  • 12