後臺管理系統三部曲之——第二部曲實現檔案的下載
阿新 • • 發佈:2020-10-16
檔案下載的實現,這個點在做的時候呼叫到了技術中臺的介面
// 影像上傳下載介面調技術中臺的 export function getDownUrl(objectId){ return requestExport({ url:`/tc-storage-service/obj/storage/download?objectId=${objectId}`, method:'get' }) }
js程式碼實現
// 下載影像上傳的檔案 downLoad(id, name) { const fileName = name; if (id) { getDownUrl(id) .then((res)=> { const link = document.createElement("a"); const blob = new Blob([res]); var reader = new FileReader(); reader.readAsText(blob, "utf-8"); reader.onload = () => { if (window.navigator && window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob); }else { link.style.display = "none"; link.href = URL.createObjectURL(blob); link.setAttribute("download", fileName); document.body.appendChild(link); link.click(); document.body.removeChild(link); } }; }) .catch((error) => { console.log(error); }); } else { this.$message({ type: "error", message: "請傳檔案路徑!", }); } },