前端请求代码:代码为项目源代码不要使用原生$.ajax({}),不支持,深入,请自行百度
axios({
method: 'post',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': loginToken
},
url: 'http://localhost:8080/download',
responseType: 'blob',
dataType: "application/json",
/*headers: { //如果需要权限下载的话,加在这里
'Authorization': loginToken
},*/
data: jsON.stringify(objects), //这里对应@RequestBody,不懂自行百度
}).then(function (res) {
var content = res.headers['content-disposition'];
var name = content && content.split(';')[1].split('filename=')[1];
var fileName = decodeURIComponent(name)
//console.log(res)
var blob = new Blob([res.data]);
var downloadElement = document.createElement('a');
var href = window.URL.createObjectURL(blob);
downloadElement.href = href;
downloadElement.download = fileName;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);
})
后端接收数据并放回文件流代码:
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public jsONResult update(@RequestBody List,Response rsp) {
//接收数据并返回对应文件格式流,略
//告诉浏览器以啥样的方式下载
rsp.setHeader("content-disposition", "attachment;filename="+file.getName());
rsp.getOutputStream().write();
return jsONResult.ok();
}
原创©本文章为庞凯博原创,未经许可,禁止转载
0条评论