使用Vue时,前端如何处理后端返回的文件流
首先,后端回到流中。 在这里,我取出了流的动作。 我在很多地方使用
//*
*下载一个文件
*
* @param docId
*/
@getmapping(/download/{docid} ) )
公共语音下载(@ path variable (docid ) ) String docId,
http服务器响应响应) {
out write (响应,文档id );
}
//*
*输出文件流
* @ param响应
* @param docId
*/
私有语音文档(httpservletresponseresponse,String docId ) {
servlet output stream out=空值;
try {
out=response.getOutputStream (;
//禁止缓存图像。
response.setheader('pragma ',' no-cache ' );
response.setheader (' cache-control ',' no-cache ' );
response.set date header (' expires ',0 );
byte [ ] bytes=doc服务. downloadfile single (docid );
If (字节!=空) {
magicmatchmatch=magic.getmagicmatch (bytes;
string mime type=match.get mime type (;
response.set内容类型(mime type;
out.write(bytes;
}
out.flush (;
}catch(exceptione ) {
UnitedLogger.error(e
} finally {
Ioutils.closequietly(out );
}
}
在前端部署了组件js-file-download
npminstalljs -文件下载--save
然后添加到Vue文件中
importfiledownloadfrom ' js-file-download ';
//文档操作列支持事件
异步操作命令(item,data ) {
sitch(item.key ) {
case 'download':
varRES=awaitthis.download(data );
returnfiledownload(RES,data.name;
.
默认:
}
//更新当前级别的列表
const folder=this.getlastfolderpath (;
this.listclick(Folder.Folderid,folder.name );
(,
//下载
同步下载
if(this.isfolder(row.type ) ) )
returnfolderapi.download (row.id;
} else {
returndocAPI.download(row.id );
}
(,
必须设置docAPI js注意响应类型
//*
*下载一个文件
* @param {*} id
*/
常数下载=(id )={
返回请求({ (
url: _DataAPI.download id、
方法: ' get ',
responseType: 'blob '
);
(;
这样下载就成功了。
有关vue.js学习教程,请单击专题vue.js组件学习教程和vue.js前端组件学习教程进行学习。
希望以上是正文的全部内容,对大家的学习有帮助。 另外,希望大家多多支持云海天教程。
原文链接: https://blog.csdn.net/QQ _ 27331631/article/details/109641254