下载文件功能,电脑下载可以正常打开,手机下载无法打开,或者打开乱码
后台代码
HttpStatus statusCode = HttpStatus.OK;
HttpHeaders headers = new HttpHeaders();
if (download) {
String fileName = new String(r.getName().getBytes(), "ISO8859-1");
headers.add("Content-Disposition", "attachment;filename=" + fileName);
}
Resource resource = resourceLoader.getResource("file:" + path + "/" + id);
InputStream in = resource.getInputStream();
byte[] body = new byte[in.available()];
in.read(body);
ResponseEntity<byte[]> streamResponse = new ResponseEntity(body, headers, statusCode);
return streamResponse;
前端下载
function handleDownload(file: any) {
console.log(file)
let a = document.createElement('a')
let event = new MouseEvent('click')
a.download = file.name
a.href = file.url
a.dispatchEvent(event)
}
handleDownload(file: any)
file.url 是什么东西?移动端对于 bloburl 和 dataurl 支持都不太行