简体   繁体   中英

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. ( ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM