Response.TransmitFile corrupting file

I have a file on my server or well in this localhost. I want to transmit it to user using this code:

        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=buylist.pdf");
        Response.TransmitFile(Server.MapPath("~/buylist.pdf"));

        Response.ContentType = "application/csv";
        Response.AppendHeader("Content-Disposition", "attachment; filename=buylist.csv");
        Response.TransmitFile(Server.MapPath("~/buylist.csv"));

I know the file I am trying to transmit our fine and I even tried adding Response.Close() but each time the file is corrupted. The csv give me the page’s HTML. I am really lost.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try putting a Response.Clear() at the top of this code, and a Response.End() and bottom.

Also, it looks like you’re trying to transmit two files in one response, and that just won’t work. It might also be two versions of the code showing in the same snippet in your question, but this is still worth mentioning: You need to pick one file to return in one response. If you absolutely must return two files at once, you’ll have to zip or tar them together first. There is no way to send two files with the same response.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x