[英]How to send mp3 file from Java Backend to frontend and play the file in the Vue.js frontend
[英]How to dowload an excel file from backend in frontend?
我想启动在我的后端生成的 excel 文件的浏览器下载,但我不知道如何传递这种响应。
app.get('/all', function (req, res) {
db.query(...) LIMIT 20000;")
.then(function (data) {
let result = []
data.forEach(element => {
result.push(element)
})
/* make the worksheet */
var ws = XLSX.utils.json_to_sheet(data);
/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "All");
XLSX.writeFile(wb, 'alltest1.xlsx'); //This saves the file in my server but I don't know how to send it as a response.
console.log('Ready');
res.send(result);
})
.catch(function (error) {
console.log("ERROR:", error)
})
})
我一直在尝试在前端创建 excel,但由于我认为数据量很大,chrome 内存不足。 我也尝试传递缓冲区,但我得到的文件似乎已损坏。
设置文件的响应头Content-Disposition和Content-Type告诉浏览器文件作为响应发送
res.setHeader('Content-Disposition',
"attachment; filename='alltest1'; filename*=UTF-8\'\'alltest1.xlsx");
res.setHeader('Content-Type', "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
并将文件内容作为响应发送
res.send(wb)
或者你也可以使用res.download 。
res.download(filePath, fileName, (err) => { if (err) console.log(err); })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.