表达 res.download()

Ger*_*áth 6 javascript express reactjs

我不知道为什么会这样,但这真的很烦人。我希望根据Express 文档下载该文件。我有下一个代码:

  //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);
  });
Run Code Online (Sandbox Code Playgroud)

文件夹结构:

  -server
    |-server.js
  -public
    |-images
      |-a.jpg
      |-b.jpg
  -src
    |-App.js
Run Code Online (Sandbox Code Playgroud)

什么也没发生,没有显示错误。有任何想法吗?

Rob*_*der 2

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