java 端
BAttachment attachment = attachmentService.getAttachment(fileId);
byte[] bData = fastdfsUploadService.fdfsDownload(attachmentService.getAttachment(fileId).getPath());
InputStream inputStream = new ByteArrayInputStream(bData);
String file_name = new String((attachment.getName()+"."+attachment.getSuffix()));
//设置响应头
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment; filename=\"" + file_name + "\"");
output = response.getOutputStream();
byte[] buf = new byte[1024];
int i = 0;
while ((i = inputStream.read(buf)) > 0) {
output.write(buf, 0, i);
}
output.flush();
java方式二
response.reset();
String file_name = new String((attachment.getName()+"."+attachment.getSuffix()));
response.setHeader("Content-Disposition", "attachment; filename="+file_name);
response.addHeader("Content-Length", "" + bData.length);
response.setContentType(attachment.getContenttype());
IOUtils.write(bData, response.getOutputStream());
vue端
return window.axios({
url: `${window.CLIENT_COLLECTION_URL}/commonapi/downloadBizFile/${id}`,
method: 'post',
responseType: 'blob'
})
const content = res;
const blob = new Blob([content]); // 构造一个blob对象来处理数据
const fileName = "POSITION_NONSTANDARD.xlsx"; // 导出文件名
// 对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
// IE10以上支持blob但是依然不支持download
if ("download" in document.createElement("a")) {
// 支持a标签download的浏览器
const link = document.createElement("a"); // 创建a标签
link.download = fileName; // a标签添加属性
link.style.display = "none";
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click(); // 执行下载
URL.revokeObjectURL(link.href); // 释放url
document.body.removeChild(link); // 释放标签
} else {
// 其他浏览器
navigator.msSaveBlob(blob, fileName);
}
上传文件
window.axios.post(`${window.CLIENT_COLLECTION_URL}/commonapi/uploadBizFile`, data, { timeout: 300000 }