From 92dc7018b4833f58919ba5b44ba4b3f9f6a74457 Mon Sep 17 00:00:00 2001 From: Binye Qi Date: Sat, 13 Jan 2024 20:51:47 +0800 Subject: [PATCH] Revert "Download verify" (#66) --- components/DownloadModal.vue | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/components/DownloadModal.vue b/components/DownloadModal.vue index 06f4ac3..8d9e995 100644 --- a/components/DownloadModal.vue +++ b/components/DownloadModal.vue @@ -110,19 +110,19 @@ export default { } const name = url.slice(url.lastIndexOf("/") + 1) this.verifyPre(url) - const blobUrl = this.xhrDownloadHandler(this.selectedVersionUrl) - // const a = document.createElement("a") - // a.setAttribute("href", blobUrl) - // // a.setAttribute("href", this.selectedVersionUrl) - // a.setAttribute("download", name) - // a.click() - // a.remove() + const blobUrl = this.xhrDownloadHandler(this.selectedVersionUrl).toString() + const a = document.createElement("a") + a.setAttribute("href", blobUrl) + // a.setAttribute("href", this.selectedVersionUrl) + a.setAttribute("download", name) + a.click() + a.remove() + URL.revokeObjectURL(blobUrl) this.$modal.hide('download-dialog') }, async verifyPre(key) { const xhr = new XMLHttpRequest() const url = Api_mirror.getBaseurl({}) + key - // xhr.open('HEAD','https://mirrors.cqu.edu.cn/test.iso', true) xhr.open('HEAD', url, true) xhr.send() xhr.onreadystatechange = await function () { @@ -135,24 +135,14 @@ export default { }, async xhrDownloadHandler(url) { const xhr = new XMLHttpRequest() - // xhr.open('GET', 'https://mirrors.cqu.edu.cn/test.iso', true) + xhr.setRequestHeader('Cookie', document.cookie) xhr.open('GET', url, true) - xhr.withCredentials = true - xhr.responseType = "blob" xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { - const blob = xhr.response - const reader = new FileReader() - reader.readAsDataURL(blob) - reader.onload = function (e) { - const a = document.createElement("a") - a.setAttribute("href", e.target.result ) - // a.setAttribute("href", this.selectedVersionUrl) - a.setAttribute("download", name) - a.click() - a.remove() - } + const data = xhr.response + const blob = new Blob([data], { type: 'application/octet-stream'}) + return URL.createObjectURL(blob) } else { console.error('request failed. ', xhr.status) return '' @@ -163,7 +153,7 @@ export default { }, addCookie(cookieName, cookieValue, path) { document.cookie = `${cookieName}=${cookieValue}; max-age=300;` - // setTimeout("location.reload()", 5000) + setTimeout("location.reload()", 5000) }, selectedChanged(newVal) { if (this.selectedCategory === 0) {