【问题标题】:Is there a way to stop IE10/IE11 from showing open or save prompt有没有办法阻止 IE10/IE11 显示打开或保存提示
【发布时间】:2017-12-18 02:43:58
【问题描述】:

我正在使用类似的 javascript blob 从服务器下载文件

let blob = new Blob([resposne['_body']], { type: contentType });
if (navigator.msSaveBlob) {
    navigator.msSaveOrOpenBlob(blob, fileName); // in case of IE
} else {
    let objectUrl = window.URL.createObjectURL(blob);
    window.open(objectUrl);
}

上面的代码运行良好,但在 IE 中显示了一个对话框:

此外,如果我在 href 标记中放置一个直接的 pdf 链接,那么它也可以正常工作。所以看起来adobe插件没有问题。

我想要的是直接打开文件而不是显示这个提示。我试过Registry hack as suggested here,但没有任何运气。知道如何实现这一目标吗?

【问题讨论】:

    标签: javascript angularjs typescript blob internet-explorer-11


    【解决方案1】:

    对于面临同样问题的任何人,我使用window.open 解决了它。我没有下载响应,而是直接将 URL 传递给 window.open 类似

    window.open(apiUrl) // Exmp "host:api/documents/download?id=1"
    

    注意:-API 应该返回带有标头类型的流响应。在我的例子中,C# Web API 方法是

    public HttpResponseMessage Download(int id)
    {
       var data = _service.Download(id);
       HttpResponseMessage result = null;
       result = Request.CreateResponse(HttpStatusCode.OK);
       result.Content = new ByteArrayContent(data);//here data is byte[]
       var name = data.Name.ToLower().Contains(data.DocType.ToLower())
                ? data.Name
                : $"{data.Name}{data.DocType}";
       result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
       {
                FileName = name
       };
       result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(name));
    //here i am setting up the headers content type for example 'text/application-json'
       return result;
    }
    

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2011-05-07
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 2015-11-03
      相关资源
      最近更新 更多