Home > Blockchain >  File Downloading Error Using Express, Axios and Js-File-Download
File Downloading Error Using Express, Axios and Js-File-Download

Time:12-21

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. 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?

CodePudding user response:

In my opinion, every Axios release from v1.0.0 has been fundamentally broken.

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("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`);
  });
  • Related