若依框架文件下载
前端:
handleDownload().then((blob) => {
const elink = document.createElement("a");
elink.download = 'xxx.xls';
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
})
export function handleDownload() {
? return request({
? ? url: '/xxx/xxx',
? ? method: 'get',
? ? responseType: 'blob'
? })
}
后端:
? ? @GetMapping("/xxx")
? ? public void downloadexce(HttpServletResponse response) throws Exception {
? ? ? ?try {
? ? ? ? ? ? String fileName = "xxx.xls";
? ? ? ? ? ? String downloadPath =? "D:/"+ fileName;
? ? ? ? ? ? File file= new File(downloadPath);
? ? ? ? ? ? if(!file.exists())
? ? ? ? ? ? ? ? throw new Exception();
? ? ? ? ? ? // 下載名稱
? ? ? ? ? ? String downloadName = fileName;
? ? ? ? ? ? response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
? ? ? ? ? ? FileUtils.setAttachmentResponseHeader(response, downloadName);
? ? ? ? ? ? FileUtils.writeBytes(downloadPath, response.getOutputStream());
? ? ? ? }
? ? ? ? catch (Exception e)
? ? ? ? {
? ? ? ? }
? ? }
總結
- 上一篇: Java 试题四
- 下一篇: oracle12 扩充表空间,oracl