簡體   English   中英

在ASP.NET中下載的文件

[英]File for download in 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個字符

我認為你可以做這樣的事情

Response.ContentType = "Application/pdf";

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

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

Response.End();

可能可行-這就是我所做的,這要歸功於Rick Strahl的博客。 個人ID使用MIME類型以編程方式確定“內容”類型,以便該代碼不特定於PDF,而僅是我:)

我做了類似的事情,它對我有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM