2010-09-14 51 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();