[英]To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript
我可以通過以下代碼將 blob 轉換為 pdf 附件以在瀏覽器中查看而無需下載
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);
}
這適用於 chrome 和 firefox 中的 pdf。 但是對於 .doc、.docx、.xlx,雖然我提供了適當的 mime 類型,但它正在下載。 (即對於 .doc 文件,我正在使用 application/msword 等)。
請注意,我正在從安全的 .net 核心 api 獲取 .doc、.docx、.xlx 數據。 有沒有其他方法可以在瀏覽器中查看word、excel文件而無需下載。
這是完全正常的行為,默認情況下您無法在瀏覽器中查看 Word 或 Excel 文檔,這就是剛剛下載文件的原因。 這取決於瀏覽器如何處理文件。 舊的 Internet Explorer 也可能只是下載 PDF 而不是顯示它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.