0

I want to download a file in my download folder . The file is in the uploads folder, both the folders are in same directory . The file name is BasicUserFile-id.jpg

/// 2nd attempt: This time I am trying to get the file by hardcoding the file name but it's also not working, instead a blank file is getting downloaded.

app.js

  Filedownload(e) {

    e.preventDefault();

    var id = this.props.params.id;

    console.log(id);

    $.ajax({
      type: 'GET',
      url: '/download/' + id,
      success: function (data, textStatus, request) {
        window.open('./downloads/ ', '_self');
      }

    });

  }


   <button type="button" value="Download File" onClick={this.Filedownload} 
   className="btn btn-success btn-lg">Download file</button> 

////server.js

app.get('/download/:id', function(req, res){
  var basic_id = req.params.id 
  var file = __dirname + '/uploads/BasicUserFile-'+ basic_id+ ".jpg";
  res.download(file); 
});

/// 2nd try .

 Filedownload(e) {

    e.preventDefault();

    $.ajax({
      type: 'GET',
      url: '/download/' ,
      success : function(data)

      {

        fileDownload(data, 'filename.jpg');
        console.log("file downloaded")


      }


    });

  }

///server

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

  var file = __dirname + '/uploads/BasicUserFile-5a093a24870eb12148262248.jpg';
  res.download(file); 
});

1 Answer 1

1

Your call on the client is for a GET but your server defines a POST. For standards compliance sake, change your server to accept a GET request like so:

app.get('/download/:id', function(req, res){
  var basic_id = req.params.id 
  var file = __dirname + '/uploads/BasicUserFile-'+ basic_id+ ".jpg";
  res.download(file); 
});

Also, it's not clear from what you've posted whether the client is constructing the right URL in any case, so make sure you double check that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.