From 19a85a054ae70f0a8869d11c641109cddd779ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A0=91=E6=9E=97?= <904079904@qq.com> Date: Fri, 5 Jul 2019 11:00:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../courses/shixunHomework/Listofworks.js | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/public/react/src/modules/courses/shixunHomework/Listofworks.js b/public/react/src/modules/courses/shixunHomework/Listofworks.js index d6950d43a..e58d2bb0e 100644 --- a/public/react/src/modules/courses/shixunHomework/Listofworks.js +++ b/public/react/src/modules/courses/shixunHomework/Listofworks.js @@ -1346,7 +1346,7 @@ class Listofworks extends Component { // } // let url = "/zip/shixun_report"; // let homeworkid = this.props.match.params.homeworkid; - axios.get((url),{headers:{'filename':'utf-8'}}).then((response) => { + axios.get((url),{headers:{'filename':'utf-8'},responseType: 'blob',}).then((response) => { console.log("1350"); console.log(response); if(response.data.status&&response.data.status===-1){ @@ -1355,20 +1355,20 @@ class Listofworks extends Component { }else if(response.data.status&&response.data.status===-2){ }else{ - window.location.href("/api"+url); - // response.blob().then(blob => { - // //关闭loading 按钮恢复正常 - // let blobUrl = window.URL.createObjectURL(blob); - // const filename = times.formatNowDate() + '.zip'; - // const aElement = document.createElement('a'); - // document.body.appendChild(aElement); - // aElement.style.display = 'none'; - // aElement.href = blobUrl; - // aElement.download = filename; - // aElement.click(); - // document.body.removeChild(aElement); - // }); - + // window.location.href("/api"+url); + console.log("开始下载zip文件") + const type='application/zip'//ZIP文件 + const blob = new Blob([response.data], { type: type }) + const downloadElement = document.createElement('a') + const href = window.URL.createObjectURL(blob) + //后台再header中传文件名 + const name = decodeURI(response.headers['content-disposition'].split('=')[1]) + downloadElement.href = href + downloadElement.download = name + document.body.appendChild(downloadElement) + downloadElement.click() + document.body.removeChild(downloadElement) // 下载完成移除元素 + window.URL.revokeObjectURL(href) // 释放掉blob对象 } }).catch((error) => { console.log(error)