1、問題描述
https://*****/drugTestReport/20230515/202305151106111386737.png
https://*****/drugTestReport/20230605/202306051540314553141.jpg
同樣結構的兩個圖片鏈接,使用window.open(url),一個是打開預覽,另一個是下載
2、解決方法,通過fetch請求url,獲取blob型別,區分情況,統一成下載,
/** * ### 適合預覽操作的 Blob 型別,需要將鏈接地址字符內容轉變成blob地址 - image/png - image/jpeg - image/gif - audio/mpeg - audio/ogg - audio/wav - video/mp4 - video/ogg ### 適合下載操作的 Blob 型別 - text/plain - text/csv - application/pdf - application/json - application/xml - application/zip - application/octet-stream */ async function downloadImg(url) { try { const res = await fetch(url); if (!res.ok) { throw new Error("fetch network response was not ok"); } const blob = await res.blob(); if ( blob.type.includes("image") || blob.type.includes("audio") || blob.type.includes("video") ) { const a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = ""; document.body.appendChild(a); a.click(); } else { window.open(url); } } catch (error) { //有些圖片url請求本身就出現了跨域等問題,目前純前端本人還無解,只能直接open console.log("catcherror", err); window.open(url); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/555290.html
標籤:其他
上一篇:基于Spark的大規模日志分析
下一篇:返回列表
