[英]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.