我正在嘗試在將下載影像的影像上實作下載按鈕。由于影像托管在 Firebase 存盤上,我使用他們的代碼變體來檢索我在官方檔案中找到的檔案:
我知道我真的很接近,我該如何觸發圖片下載?此外,我是否真的需要從 firebase 呼叫它,因為影像已經顯示在網站中?
解決方案:
感謝 Renaud 的幫助,我能夠修復我的代碼:
download(url) {
const xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = () => {
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL(xhr.response);
const link = document.createElement("a");
link.href = imageUrl;
link.setAttribute("download", "test");
link.setAttribute("target", "new");
document.body.appendChild(link);
link.click();
};
xhr.open("GET", url);
xhr.send();
},
請隨時發布對此解決方案的優化。
uj5u.com熱心網友回復:
一種可能的方法是在頁面中創建一個隱藏鏈接并模擬點擊該鏈接,如下所示:
// Get the URL. You have it since you call download(url)
// In case you don't have the URL, use const url = await getDownloadURL(fileRef);
const url = ...
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', fileName);
link.setAttribute('target', 'new');
document.body.appendChild(link);
link.click();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/366001.html
