2

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.

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57
  • Is that just a mistype that in the second block you're setting the filename to the PDF instead of the CSV? – xcud Feb 10 '12 at 16:36

1 Answers1

6

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794