我如何在前端处理 res.download(pathname) (ReactJS)

How do I handle res.download(pathname) on frontend(ReactJS)

我在我的服务器 (NodeJS) 上编写了一个代码,它将使用 express res.download(path) 将 .csv 文件发送到前端。我想知道如何使用 fetch API 处理响应,这将允许前端下载发送的文件 (.csv)。

我在前端使用 reactJs。

我可以通过在我的前端使用 downloadJs 来解决这个问题

import download from 'downloadjs'
...
//get request
fetch(url).then(res => res.blob())
.then(blob => download(blob, 'name of file', 'data type')) // this line automatically starts a download operation
.catch(err => console.log(err));

希望对您有所帮助