Javascript 有没有办法阻止IE10/IE11显示打开或保存提示
我使用javascript blob从服务器下载文件,比如Javascript 有没有办法阻止IE10/IE11显示打开或保存提示,javascript,angularjs,typescript,blob,internet-explorer-11,Javascript,Angularjs,Typescript,Blob,Internet Explorer 11,我使用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);
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);
}
window.open(apiUrl) // Exmp "host:api/documents/download?id=1"
上面的代码工作正常,但在IE中显示了一个对话框:
另外,如果我在href标签中添加了一个直接的pdf链接,那么它也可以正常工作。所以看起来adobe插件没有问题
我想要的是直接打开文件,而不是显示此提示。我试过了,但运气不好。你知道如何做到这一点吗 对于任何面临相同问题的人,我使用
window.open解决了这个问题。我没有下载响应,而是直接将URL传递到窗口
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);
}
window.open(apiUrl) // Exmp "host:api/documents/download?id=1"
注意:-API应返回设置了头类型的流响应。在我的例子中,C#WebAPI方法是
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;
}
希望它能帮助别人