Cal*_*lin 3 asp.net-mvc asp.net-mvc-4
我有以下代码,部署在https Asp站点上,使用MVC 4.0构建:
public FileResult ANotSoWorkingFunction(string filePath, string fileName)
{
pathToFile = string.Format("~/{0}/{1}", pathToFile, fileName);
return File(new FileStream(pathToFile, FileMode.Open), "application/pdf", fileName);
}
Run Code Online (Sandbox Code Playgroud)
这对于Chrome,Firefox和IE9来说都是可行的(正如你们许多人可能已经猜到的那样).但它会抛出一个:
---------------------------
Windows Internet Explorer
---------------------------
Internet Explorer cannot download someFileName from a_site.com.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.
---------------------------
OK
---------------------------
Run Code Online (Sandbox Code Playgroud)
在IE6,7,8上
非常感谢任何关于这个的想法或线索,因为我已经花了很多时间玩html标题.
编辑:
以下是IE7的标题:
HTTP/1.1 200 OK
Cache-Control: private, no-cache="Set-Cookie"
Content-Type: application/pdf
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 21:00:00 GMT; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Wed, 04 Apr 2012 08:43:50 GMT
Content-Length: 233324
Run Code Online (Sandbox Code Playgroud)
这是来自IE9的那些:
HTTP/1.1 200 OK
Cache-Control: private, no-cache="Set-Cookie"
Content-Type: application/pdf
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 21:00:00 GMT; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Wed, 04 Apr 2012 08:42:14 GMT
Content-Length: 233324
Run Code Online (Sandbox Code Playgroud)
谢谢,
我想我也遇到了你的问题.
我还运行IIS 7.5并通过HTTPS请求上的操作下载PDF.由于我尚未隔离的原因,IIS 7.5似乎附加no-cache="Set-Cookie"到我的Cache-Control响应头,无论我在响应上设置缓存设置.这导致了no-cacheIE6,IE7和IE8上相当完整的文档问题.
为了解决这个问题,我在FileContentResult周围做了一个小包装,清除了标题,称为父标题,然后将Cacheability设置为'Private'.这方面踩了IIS 7.5坚持添加no-cache="Set-Cookie"到标题,并在我测试的所有浏览器中正确下载文件.如果你想模仿我所做的,首先,这是我的FileContentResult包装器.
public class PdfContentResult : FileContentResult {
public PdfContentResult(byte[] data) : base(data, "application/pdf") { }
public PdfContentResult(byte[] data, string fileName) : this(data) {
if (fileName == null) {
throw new ArgumentNullException("fileName");
}
this.FileDownloadName = fileName;
}
public override void ExecuteResult(ControllerContext context) {
context.HttpContext.Response.ClearHeaders();
base.ExecuteResult(context);
context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Private);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我为我添加了一个扩展方法,ControllerExtensions以便找到它很简单:
public static class ControllerExtensions {
public static PdfContentResult Pdf(this Controller controller, byte[] fileContents, string fileName) {
return new PdfContentResult(fileContents, fileName);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,在Action中,我做了相同的事情:
public ActionResult MyGeneratedPdf() {
byte[] myPdfContentInByteStream = GetPdfFromModel();
return this.Pdf(myPdfContentInByteStream, "MyFile.pdf");
}
Run Code Online (Sandbox Code Playgroud)
显然,如果您正在下载各种数据类型,您可能不希望将解决方法与PDF紧密绑定.
| 归档时间: |
|
| 查看次数: |
7874 次 |
| 最近记录: |