加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Asp.Net MVC 4 API:在IE8中下载docx失败

发布时间:2020-12-15 08:37:06 所属栏目:百科 来源:网络整理
导读:我正在将文档存储在数据库中,并具有用于下载文档的API. docx和xlsx的下载在IE9,Chrome和FF中运行良好但在真正的IE8中失败.(IE8模式下的IE 9也可以) 我得到的错误消息如下: Unable to download 393 from idler2. Unable to open this Internet site. The req
我正在将文档存储在数据库中,并具有用于下载文档的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.

使用以下响应标头:
HTTP / 1.1 200好的
缓存控制:无缓存
Pragma:没有缓存

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;
}

我在内容配置和内容标题上尝试了很多变化,但没有运气..

解决方法

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

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

在MVC Web API中,它实际上又迈出了一步.由于您要创建新的HttpResponseMessage,因此还必须在消息的标题上创建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;
}

我有确切的问题,但这解决了它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读