我目前正在嘗試從我的 Sapui5 應用程式下載 PNG 檔案。我嘗試了幾種方法,但它們都只在新的或相同的選項卡中顯示影像。從未觸發下載。
我已經嘗試過這些方法:
1) URLHelper.redirect
2) window.open,
3) window.location.assign,
4) fetch(URL).then(res => res.blob()).then(blob => {
var file = window.URL.createObjectURL(blob);
window.location.assign(file);
5) var link = document.createElement("a");
link.href = FullURL;
link.download = "QRCode.png";
link.target = "_self";
document.body.appendChild(link);
link.click();
document.body.removeChild(link)
6) sap.ui.core.util.File.save
<---- 這只會導致檔案作為可能不支持檔案格式的訊息回傳。
如何觸發下載?
我對此很陌生,希望你們能幫助我。謝謝
uj5u.com熱心網友回復:
解決方案 #5 僅適用于影像和 URL 具有相同來源(協議、子域、域和埠必須相同)的情況。
例外是blob和dataURL。因此,您可以將解決方案#5 與#4 結合起來。
我創建了一個小的作業示例。
相關代碼在這里:
const sURL = 'https://i.imgur.com/q6E7b9d.png';
fetch(sURL)
.then((oResponse) => oResponse.blob())
.then((oBlob) => {
const sBlobURL = URL.createObjectURL(oBlob);
const oLink = document.createElement('a');
oLink.href = sBlobURL;
oLink.download = sURL.split('/').pop();
oLink.target = '_blank';
document.body.appendChild(oLink);
oLink.click();
document.body.removeChild(oLink);
});
如果這也不起作用,那么您可能遇到了 CORS 問題。基本上,影像的主機決定影像是否可以嵌入和下載到其他頁面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458924.html
標籤:javascript 文件 下载 sapui5 PNG
