简体   繁体   English

在ASP.NET中下载的文件

[英]File for download in ASP.NET

I have a "dummy" page to force the "SaveAs" pop-up when they click a link to download a document. 我有一个“虚拟”页面,当他们单击链接下载文档时,会强制弹出“另存为”。

the request for that site is ajax, and the site is called. 该站点的请求是ajax,并且该站点被调用。 I can se that it got all the right params, but when it comes to this part nothing happens. 我可以确定它具有所有正确的参数,但是当涉及到这部分时,什么也没有发生。

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

with firebug, I can see that i tries to response something, if I look at the header. 使用萤火虫,我可以看到如果尝试查看标题,我会尝试做出响应。

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

but if I look at the response, it looks like 但是如果我看一下响应,它看起来像

%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�

and so on for the next 785600 characters 以此类推,接下来的785600个字符

i think you can do something like this 我认为你可以做这样的事情

Response.ContentType = "Application/pdf";

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

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

Response.End();

and that may work - this is what I have done and is thanks to Rick Strahl's blog. 可能可行-这就是我所做的,这要归功于Rick Strahl的博客。 Personally Id determine the Content type programatically using the MIME type so that the code isnt specifi to PDF but thats just me :) 个人ID使用MIME类型以编程方式确定“内容”类型,以便该代码不特定于PDF,而仅是我:)

I do something similar, and it works for me: 我做了类似的事情,它对我有用:

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