springboot vue 下载文件,浏览器未显示下载的文件
只有控制台 Preview 有文件
@PostMapping("/down")
public void down(HttpServletResponse response, @Param("id") int id) {
Backup backup = backupService.getById(id);
File file=new File(backup.getPath());
response.setContentType ("application/octet-stream");
String name = file.getName()+".txt";
try {
name = new String (name.getBytes (StandardCharsets.UTF_8), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace ();
}
response.addHeader ("Content-Disposition", "attachment;filename=" + name);
OutputStream opmt = null;
try {
opmt = response.getOutputStream ();
opmt.write (FileUtils.readFileToByteArray (new File (backup.getPath())));
opmt.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
async download({id}){ // id=
const result = await getBackupDown({id}).catch(err=>{
this.$message.error('网络请求错误')
})
if(result.status){
this.$message.error('下载失败')
}else {
this.$message.success('下载成功')
}
await this.fetchData()
},
<a-button value="small" @click="download(item)">下载</a-button>