gpt4 book ai didi

c# - Asp.Net MVC 4 API : Download of docx failing in IE8

转载 作者:太空狗 更新时间:2023-10-29 21:09:32 26 4
gpt4 key购买 nike

我将文档存储在数据库中,并有一个用于下载文档的 API。

docx 和 xlsx 的下载在 IE9、Chrome 和 FF 中工作正常,但在真正的 IE8 中失败。(IE8 模式下的 IE 9 也可以)

我得到的错误信息如下:

Unable to download 393 from idler2.

Unable to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

具有以下响应 header :HTTP/1.1 200 正常缓存控制:无缓存用法:无缓存

Content-Length: 10255
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename=document.docx
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 23 Mar 2013 11:30:41 GMT

这是我的 api 方法:

public HttpResponseMessage GetDocumentContent(int id)
{
Document document = Repository.StorageFor<Client>().GetDocument(id);
HttpResponseMessage response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Content = new ByteArrayContent(document.GetBuffer());
response.Content.Headers.ContentLength = document.GetBuffer().Length;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
//FileName = document.GetFileName(),
FileName = "document.docx",
DispositionType = "attachment"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
}

我已经尝试了很多关于内容配置和内容 header 的变体,但没有成功......

最佳答案

我假设您在 SSL 下遇到过这种情况。如果是这样,那么这是一个 known issue .本文讨论的是 Office 文档,但此问题适用于所有文件类型。

那篇文章的解决方案是删除 no-cache header ,但还有更多内容。当 IE8 通过 SSL 与网站通信时,IE8 会强制执行任何无缓存请求。如果 header 或 header 存在,IE8 不会缓存该文件。因此它无法打开文件。所有这些都是 IE5 到 IE8 特有的。

在 MVC Web API 中,它实际上采取了另一个步骤。由于您正在创建一个新的 HttpResponseMessage,因此您还必须在消息的 header 上创建一个 CacheControlHeaderValue。您不必设置任何标题属性,只需实例化一个新属性即可。标题将默认为所需内容,因此您不必更改属性。

public HttpResponseMessage GetDocumentContent(int id)
{
Document document = Repository.StorageFor<Client>().GetDocument(id);
HttpResponseMessage response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Headers.CacheControl = new CacheControlHeaderValue(); // REQUIRED
response.Content = new ByteArrayContent(document.GetBuffer());
response.Content.Headers.ContentLength = document.GetBuffer().Length;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "document.docx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
}

我遇到了确切的问题,但这解决了它。

关于c# - Asp.Net MVC 4 API : Download of docx failing in IE8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15586309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com