Express res.download() [英] Express res.download()

查看:354
本文介绍了Express res.download()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么会这样,但这确实很烦人.我希望根据表达文档来下载文件.我有下一个代码:

I don't know why it's happening, but it's really annoying. I expected the file to be downloaded according to the express docs. I have the next code:

  //in react (App.js)
  download = () => {
    const { images, target } = this.state;
    const filename = images.find(img => img.id === target).filename;
    fetch('http://localhost:3001/download', {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({filename})
    })
  }
  //in express (server.js)
  app.post('/download', (req, res) => {
    var file = '../public/images/' + req.body.filename;
    res.download(file, req.body.filename);
  });

文件夹结构:

  -server
    |-server.js
  -public
    |-images
      |-a.jpg
      |-b.jpg
  -src
    |-App.js

什么都没发生,错误未显示.有什么想法吗?

Nothing's happening, errors not shown. Any ideas?

推荐答案

我认为您遇到的问题是您使用的是HTTP POST而不是HTTP GET

I think the issue you have is that you're using an HTTP POST instead of HTTP GET

这篇关于Express res.download()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆