0

I am trying to download a string into CSV file with the following code:

string attachment = "attachment; filename=Test.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.Write("my,csv,file,contents");
HttpContext.Current.Response.End();

My Fiddler capture gave me the following:

 HTTP/1.1 200 OK
 Server: ASP.NET Development Server/10.0.0.0
 Date: Wed, 18 Sep 2013 22:06:19 GMT
 X-AspNet-Version: 4.0.30319
 content-disposition: attachment; filename=Test.csv
 Pragma: public
 Cache-Control: private
 Content-Type: text/csv; charset=utf-8
 Content-Length: 20
 Connection: Close

 my,csv,file,contents

But, the browser is not downloading any CSV file. Please help. Thanks,

2 Answers 2

0

The header should be written as Content-Disposition - note the capital letters in the name. For more information, search for 19.5.1 Content-Disposition in http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

For example:

Content-Disposition: attachment; filename="fname.ext"

Also note the double quotes around the file name.

Responding with a csv file in asp.net

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

1 Comment

The issue was not this. I have found the answer and have posted it. Thanks for your response.
0

The issue was an exception: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

Found a useful article here

and found a solution here

Thanks,

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.