文件没有在express中下载并做出反应

时间:2017-11-16 05:25:19

标签: node.js ajax reactjs express

我想从文件夹下载文件到浏览器。我的第一个代码工作正常。文件立即下载,但当我尝试通过id特定地找到该文件时,它不起作用。不知道为什么?

///客户端工作

      $.ajax({
      type: 'GET' ,
      url: '/download' ,



      success : function()

      {
        window.open('/download?foo=bar&xxx=yyy');

      }


    });   

/////服务器工作

    app.get('/download', function(req, res) {



      res.download(__dirname + '/uploads/google.png');
    });

////客户端不起作用

          $.ajax({
          type: 'GET' ,
          url: '/download/' + id ,



          success : function()

          {
            window.open('/download?foo=bar&xxx=yyy');

          }


        });

/////服务器无法正常工作

      app.get('/download/:id', function(req, res) {

      var id  = req.params.id

      console.log("ddddddddddddddddddddddddddddddddddddddddd")

      console.log(id)

      res.download(__dirname + '/uploads/google' + id  +'.png');
    });

1 个答案:

答案 0 :(得分:1)

因为您将值作为查询参数传递而不是来自客户端的URL参数。

服务器端

app.get('/download', function(req, res) {

  var foo  = req.query.foo;
  var xxx = req.query.xxx;

  console.log(foo)
  console.log(xxx)

  //res.download(__dirname + '/uploads/google' + id  +'.png');
});
相关问题