From 6df310eda1ca99a10bc7eb8a524d992ed6878b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A0=91=E6=98=8E?= <775174143@qq.com> Date: Sat, 6 Jul 2019 16:14:04 +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 --- .../modules/courses/Resource/Fileslistitem.js | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/public/react/src/modules/courses/Resource/Fileslistitem.js b/public/react/src/modules/courses/Resource/Fileslistitem.js index fef839e88..57c69f837 100644 --- a/public/react/src/modules/courses/Resource/Fileslistitem.js +++ b/public/react/src/modules/courses/Resource/Fileslistitem.js @@ -28,16 +28,24 @@ class Fileslistitem extends Component{ this.props.Settingtypes(discussMessage.id) } - downloadfiles=(url,name)=>{ - const aLink = document.createElement('a'); - aLink.style.display='none'; - console.log(name) - aLink.setAttribute('download',name) - aLink.href = url; - document.body.appendChild(aLink); - aLink.click(); - // document.body.removeChild(aLink); + downloadfiles=(url)=>{ + axios.get((url),{responseType: 'blob'}).then((response) => { + const blob = new Blob([response.data]); + const downloadElement = document.createElement('a'); + const href = window.URL.createObjectURL(blob); + // const string = Base64.decode(response.headers['content-disposition'].split('=')[1]); + const string = response.headers['content-disposition'].split('=')[1]; + downloadElement.href = href; + downloadElement.download = string; + document.body.appendChild(downloadElement); + downloadElement.click(); + document.body.removeChild(downloadElement) ;// 下载完成移除元素 + window.URL.revokeObjectURL(href) // 释放掉blob对象 + }).catch((error) => { + console.log(error) + }); } + showfiles=(value)=>{ let {discussMessage,coursesId}=this.props let file_id=discussMessage.id @@ -47,16 +55,14 @@ class Fileslistitem extends Component{ course_id:coursesId }, }).then((result)=>{ - if(result.data.attachment_histories.length===0){ - this.downloadfiles(result.data.url,result.data.title) + this.downloadfiles(result.data.url) }else{ this.setState({ Showoldfiles:true, allfiles:result.data }) } - }).catch((error)=>{ console.log(error) })