Force browser to download PDF document instead of opening it

后端 未结 5 1577
太阳男子
太阳男子 2020-12-01 12:14

I want to make the browser download a PDF document from server instead of opening the file in browser itself. I am using C#.

Below is my sample code which I used. It

5条回答
  •  温柔的废话
    2020-12-01 13:00

    You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=foo.pdf" will prompt the user (typically) with a "Save as: foo.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile for this purpose. For example (assuming you aren't using MVC, which has other preferred options):

    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
    Response.TransmitFile(filePath);
    Response.End(); 
    

提交回复
热议问题