From 81506cc3372c2022fb5e38b42bb2b041584a9909 Mon Sep 17 00:00:00 2001 From: wangxiaoshuang <825034831@qq.com> Date: Wed, 17 Jul 2024 16:49:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E6=94=B9?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/uploadZip.js | 68 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/src/utils/uploadZip.js b/src/utils/uploadZip.js index 3b0ded22..2844e3da 100644 --- a/src/utils/uploadZip.js +++ b/src/utils/uploadZip.js @@ -24,18 +24,20 @@ export const downloadImage = async (id, id2, IsDicom = true) => { } let a = document.createElement("a"); let href = Vue.prototype.OSSclientConfig.basePath + res.Result; - // let fileName = - // res.Result.split("/")[res.Result.split("/").length - 1]; - a.download = res.OtherInfo.fileName; - a.href = href; - a.click(); - URL.revokeObjectURL(href); - let timer = setTimeout(() => { - a = null; - href = null; - timer = null; - }, 500) + download(href, res.OtherInfo.FileName) return 2; + // // let fileName = + // // res.Result.split("/")[res.Result.split("/").length - 1]; + // a.download = res.OtherInfo.FileName; + // a.href = href; + // a.click(); + // URL.revokeObjectURL(href); + // let timer = setTimeout(() => { + // a = null; + // href = null; + // timer = null; + // }, 500) + // return 2; } else { return false; } @@ -142,4 +144,46 @@ export const fileDownload = (content, filename) => { document.body.appendChild(eleLink); eleLink.click(); document.body.removeChild(eleLink); -}; \ No newline at end of file +}; +let download = async (downloadUrl, downloadFileName) => { + const blob = await getBlob(downloadUrl); + saveAsB(blob, downloadFileName); +} + +let getBlob = (url) => { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + + xhr.open('GET', url, true); + xhr.responseType = 'blob'; + xhr.onload = () => { + if (xhr.status === 200) { + resolve(xhr.response); + } else { + reject(new Error(`Request failed with status ${xhr.status}`)); + } + }; + xhr.onerror = () => { + reject(new Error('Request failed')); + }; + + xhr.send(); + }); +} + +let saveAsB = (blob, filename) => { + const link = document.createElement('a'); + const body = document.body; + + link.href = window.URL.createObjectURL(blob); + link.download = filename; + + // hide the link + link.style.display = 'none'; + body.appendChild(link); + + link.click(); + body.removeChild(link); + + window.URL.revokeObjectURL(link.href); +} \ No newline at end of file