Search code examples
javascriptangularjstypescriptblobinternet-explorer-11

Is there a way to stop IE10/IE11 from showing open or save prompt


I am downloading files from server using javascript blob something like

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

The code above works fine but in IE it shows a dialog:

prompt

Also if I put a direct pdf link in the href tag then it also works fine. So it looks like there is no problem with the adobe plugin.

What I want is to directly open the file instead of showing this prompt. I tried Registry hack as suggested here but did not have any luck. Any idea how to achieve this?


Solution

  • For anyone who is facing the same issue, I solved it by using window.open. Instead of downloading the response, I directly pass the URL to window.open something like

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

    Note:-API should returns the stream response with header type set. In my case C# web API method was

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

    Hope it will help someone.