2

I want to be able to create a text file and let the user save this to their own PC. Currently i have to do this:

StreamWriter sw;
sw = File.CreateText("C:\\results.txt");

Which obv saves on the web server but how can I replace that string with a link to their own computer? I looked at SaveFileDialog but it seems to only work for Windows Forms and not ASP sites, any advice?

Thanks in advance

samcooper11
  • 265
  • 2
  • 10
  • 20

4 Answers4

5

You need to tell the Users Browser to Download the file. You can use the following code:

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType="text/plain";
Response.AppendHeader( "content-disposition", "attachment; filename=" + filename );
Response.AppendHeader( "content-length", fileContents.Length );
Response.BinaryWrite( fileContents );
Response.Flush();
Response.End();

Where fileContents is a byte[] of the files contents. and filename is the name of the file to suggest to the user.

FallenAvatar
  • 4,079
  • 1
  • 21
  • 24
2

You need to write an ASPX (or ASHX) page that sends your data down the wire, and add a Content-Disposition header.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

You can stream the file to their browser. Here is a good article.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
1

You need to save the file to a publicly accessible path and then present the user with a link to the file.

Shaun Mason
  • 787
  • 4
  • 14