Uploading a file sent in response to a POST request via nodejs?

I have a problem issuing a POST command that downloads a file.

On the client side, I am trying to send POST to a specific URL, including a parameter that indicates the file to download.

var req = $.ajax({
    type: 'POST',
    url : '/click',
    data: { 'path' : filename }
   });
req.done(function(data) {
// Download the file here?

The server eventually disables the method that does this:

function downloadFile(req, res) {
  var dir = req.session.currentdir + req.body.path;
  mimetype = (shell.exec("file --mime-type '" + dir + "'", {silent:true}).output);
  mimetype = mimetype.substring(mimetype.indexOf(": ") + 2, mimetype.length);

  var stat = fs.statSync(dir);
  res.writeHead(200, {'Content-Type' : mimetype,
                      'Content-Length': stat.size });
  var fileStream = fs.createReadStream(dir);
  fileStream.pipe(res);
};

Now I can not get the client side to accept the file I'm trying to execute. It just hangs a very long time before closing. How can I get the client to upload the file I'm trying to send back?

Thank you very much for your time reading.

+3
source share
1 answer

1. resp.setHeader( "Content-Disposition", "attachment; filename = \" xxxx.xxx\"");

2. Get

+5

All Articles