2010-09-14 32 views
0

我有一个“虚拟”页面,当他们单击链接下载文档时强制弹出“另存为”页面。在ASP.NET中下载的文件

该网站的请求是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个字符

回答

1

我认为你可以做这样的事情

Response.ContentType = "Application/pdf"; 

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

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

Response.End(); 

这可能会奏效 - 这正是我所做的,感谢Rick Strahl的博客。个人标识确定内容类型编程使用MIME类型,这样的代码心不是specifi到PDF,但是那只是我:)

1

我做同样的事情,它的工作对我来说:

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
Response.TransmitFile(Server.MapPath(FilePath)); 
Response.End();