微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ReactJs - 检索并下载 ZIP 文件,调用 api nodeJS 的结果

如何解决ReactJs - 检索并下载 ZIP 文件,调用 api nodeJS 的结果

我有一个 api nodeJs,响应是一个 zip 文件

exports.zip = async function(req,res) {
  var dirPath = path.resolve(__dirname,'../data/test.zip');

  await res.zip({
    files: [
      { content: 'this is a string',//options can refer to [http://archiverjs.com/zip-stream/ZipStream.html#entry](http://archiverjs.com/zip-stream/ZipStream.html#entry)
           name: 'file-name',mode: 0755,comment: 'comment-for-the-file',date: new Date(),type: 'file' },{ path: path.join(dirPath,'test'),name: 'test' },//can be a file
      
  ],filename: 'test.zip'
  });

我的路线是:

app.route('/zip').post(homeController.zip);

使用邮递员进行测试时,我得到了带有“另存为文件”选项的 zip 文件

在我的react前端,需要直接下载并保存API的响应文件

我该怎么做? 谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。