摘要:本篇文章將從一個實際項目出發,分享如何使用 Spark 進行大規模日誌分析,並通過代碼演示加深讀者的理解。 本文分享自華為雲社區《【實戰經驗分享】基於Spark的大規模日誌分析【上進小菜豬大數據系列】》,作者:上進小菜豬。 隨著互聯網的普及和應用範圍的擴大,越來越多的應用場景需要對海量數據進行 ...
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); } }