强制浏览器下载PDF文档,而不是打开它 [英] Force browser to download PDF document instead of opening it

查看:394
本文介绍了强制浏览器下载PDF文档,而不是打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使浏览器从服务器上下载一个PDF文档,而不是浏览器本身打开的文件。我使用C#。

下面是我的样本code,我使用。它不工作。

 字符串文件名=样本服务器URL;
Response.Redirect的(文件名);


解决方案

您应该看一下内容处置标头;例如,设置内容处置到附件;文件名= foo.pdf会提示用户(通常)用另存为:foo.pdf对话框,而不是打开它。但是,这需要来自这是请求的做的下载,所以你不能重定向过程中做到这一点。但是,ASP.NET报价 Response.TransmitFile 用于这一目的。例如(假设你不使用MVC,里面有其他的preferred选项):

  Response.Clear();
Response.ContentType =应用程序/ PDF
Response.AppendHeader(内容处理,附件;文件名= foo.pdf);
Response.TransmitFile(文件路径);
到Response.End();

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 not working..

string filename = "Sample server url";
response.redirect(filename);

解决方案

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(); 

这篇关于强制浏览器下载PDF文档,而不是打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆