Ehasanul Hoque
Ehasanul Hoque

Reputation: 640

To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript

I am able to convert blob into pdf attachment to view in browser without download by following code

 var ieEDGE = navigator.userAgent.match(/Edge/g);
        var ie = navigator.userAgent.match(/.NET/g); // IE 11+
        var oldIE = navigator.userAgent.match(/MSIE/g);
        //var bytes = new Uint8Array(response); //use this if data is raw bytes else directly pass resData
        var blob = new window.Blob([response], { type: 'application/pdf' });

        if (ie || oldIE || ieEDGE) {
          window.navigator.msSaveBlob(blob, fileName);
        }
        else {
          var fileURL = URL.createObjectURL(blob);
          window.open(fileURL);
        }

This is working for pdf in chrome and firefox. But for .doc, .docx,.xlx, it is downloading though i am providing appropriate mime type. ( i.e. for .doc file I am using application/msword and so on).

Note that I am fetching .doc,.docx,.xlx data from secure .net core api. Is there any other way to view word, excel file in browser without download.

Upvotes: 0

Views: 2830

Answers (1)

SteapStepper69
SteapStepper69

Reputation: 1295

This is totally normal behavior, you can't view Word or Excel Documents in your browser by default, that's why the file is just being downloaded. It depends on the browser how files are treated. Old Internet Explorer might just download the PDF too instead of showing it.

Upvotes: 2

Related Questions