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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.