实现思路:点击下载按钮请求后端接口,获取文件流和下载时文件显示的名称,a标签触发
<span class="table_look" @click="toDownload(scope.$index,scope.row)" >下载</span>
<a ref="download" :href="downloadUrl" :download="downloadfilename" style="display: none;"></a>
toDownload(index,m){
var _this = this;
var params = {
fileId:m.s_FileID
}
this.$http.get('/API/FileAPI/DownloadFileMethod',{
params:params
})
.then(function(res){
if(res.status == 200){
//获取文件名称的后缀
var str = res.data.fileName;
var index = str.lastIndexOf(".");
str = str.substring(index + 1, str.length);
//判断文件类型,和后端给的文件流拼接(若返回的是完整的流,即带有文件类型。则,可直接下载)
if(str == 'docx'){
_this.downloadUrl = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,'+ res.data.fileBase64Content
}else if(str == 'doc'){
_this.downloadUrl = 'data:application/msword;base64,'+ res.data.fileBase64Content
}else if(str == 'txt'){
_this.downloadUrl = 'data:text/plain;base64,' + res.data.fileBase64Content
}else{
}
_this.downloadfilename = res.data.fileName;
setTimeout(() => {
_this.$refs.download.click();
}, 1000)
}else{
}
})
.catch(error =>{
})
},
上传文件完整的文件流
下载时返回的文件流