Creating and saving a text file from a c# ASP.NET app
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 thi开发者_开发问答s:
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
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.
You need to write an ASPX (or ASHX) page that sends your data down the wire, and add a Content-Disposition
header.
You can stream the file to their browser. Here is a good article.
You need to save the file to a publicly accessible path and then present the user with a link to the file.
精彩评论