Nahid Hasan
Nahid Hasan

Reputation: 707

file not dowloading in express and react

i want to download file from folder to browser . my first code works fine .the file gets downloaded immediately but when i try to find the file specificly by id it does not works . dont know why ?

///client works

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



      success : function()

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

      }


    });   

/////server works

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



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

////client not works

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



          success : function()

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

          }


        });

/////server not working

      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');
    });

Upvotes: 0

Views: 85

Answers (1)

Murtuza Z
Murtuza Z

Reputation: 6017

Because you are passing values as query parameters and not URL parameter from client.

Server side

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');
});

Upvotes: 1

Related Questions