[英]File Downloading Error Using Express, Axios and Js-File-Download
I made a simple downloadable response from my express server.我从我的快递服务器做了一个简单的可下载响应。
app.post("/download", (req, res) => {
let file_name = req.body.name;
res.download(path.join(__dirname, `files/${file_name}.mp3`), (err) => {
console.log(err);
});
});
And I used axios and js-file-download to download the responsed file from frontend.我使用 axios 和 js-file-download 从前端下载响应文件。 The file is donwloaded with full file size.
该文件以完整文件大小下载。 But it's not playable.
但它不能播放。
Axios.post("http://localhost:3001/download", { name: name }).then(
(response) => {
fileDownload(response.data, `${title}.mp3`);
}
);
How can I solve this problem?我怎么解决这个问题?
In my opinion, every Axios release from v1.0.0 has been fundamentally broken.在我看来,从 v1.0.0 开始的每个 Axios 版本都从根本上被破坏了。
The request you're making is trivially easy using the built-in Fetch API and won't be subject to Axios' poor testing and broken releases.使用内置的 Fetch API,您发出的请求非常容易,并且不会受到 Axios 糟糕的测试和损坏的版本的影响。
fetch("http://localhost:3001/download", {
method: "POST",
body: JSON.stringify({ name }),
headers: { "content-type": "application/json" },
})
.then((res) => (res.ok ? res.blob() : Promise.reject(res)))
.then((file) => {
fileDownload(file, `${title}.mp3`);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.