0

当他们单击链接下载文档时,我有一个“虚拟”页面来强制弹出“另存为”。

该站点的请求是ajax,并且该站点被调用。我可以确定它得到了所有正确的参数,但是当涉及到这部分时,什么也没有发生。

var filepath = Request["filepath"];

//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath(filepath);
Response.AppendHeader("content-disposition", "attachment; filename=" + FilePath);
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
esponse.End();

使用萤火虫,如果我查看标题,我可以看到我试图响应某些内容。

Server ASP.NET Development Server/10.0.0.0
Date Tue, 14 Sep 2010 12:51:02 GMT
X-AspNet-Version 4.0.30319
Content-Disposition attachment; filename=D:\APPLICATIONS\InfoomFilm.pdf
Cache-Control private
Content-Type Application/pdf
Content-Length 785693
Connection Close

但如果我看一下回复,它看起来像

%PDF-1.3
%���������
4 0 obj
<< /Length 5 0 R /Filter /FlateDecode >>
stream
x��ْ��u���)pي k��w�,��($þp袇,rz�lR���z�~��+8�/��$�Ld�P�,rȑ"�'��%d���s�ע,�mi��}9�}Wt�n[C[m���P�WqW|��CU<�����s{m[���f����a�

等等接下来的 785600 个字符

4

2 回答 2

1

我想你可以做这样的事情

Response.ContentType = "Application/pdf";

Response.AppendHeader("content-disposition", "attachment; filename=" + FilePath);

Response.TransmitFile( Server.MapPath(filepath) );

Response.End();

这可能会奏效 - 这就是我所做的,这要感谢 Rick Strahl 的博客。个人 Id 使用 MIME 类型以编程方式确定内容类型,以便代码不是特定于 PDF 的,但这只是我 :)

于 2010-09-14T13:24:22.537 回答
1

我做了类似的事情,它对我有用:

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(Server.MapPath(FilePath));
Response.End();
于 2010-09-14T14:04:57.103 回答