简体   繁体   中英

upload a file in nodejs server with sails.js

I created a server using sails.js on node with an end-point to upload files on the server by following the example in this page .

Here the complete code:

req.file('file').upload({
  maxBytes: 10000000,
  dirname: './my-dir'
}, function whenDone (err, uploadedFiles) {
  var fs = require('fs');

  if (err) {
    return res.negotiate(err);
  }

  // If no files were uploaded, respond with an error.
  if (uploadedFiles.length === 0){
    return res.badRequest('No file was uploaded');
  }

  console.log('file exists:', fs.existsSync(uploadedFiles[0].fd));

  var path = uploadedFiles[0].fd;
  var tmpFileName = /[^/]*$/.exec(path)[0];
  var url = '/my-dir/'+tmpFileName;

  return res.send(url);
});

The console.log line was added just to be sure that the file exists when the function whenDone is called (and the file exists already for every single call).

The problem is that on client-side: when the page sends a file to this end-point and receives the response from the server, it try to send another request to the same uploaded file (generally an image) and it receive a 404.

With the console.log I checked that the file exists and I'm sure that the url is correct for if I try to access the file with the url the server returns the file, and also if I add a setTimeout on client-side code works for 90% of times (it means that it's a timing issue).

Is there some sails.js event fired when the file is not just loaded but also ready to be sent to the client?

Thanks in advance.

Static assets (uploaded files in your case) are fetched from the .tmp directory in your project root. When sails is lifted, .tmp is cleaned up and populated with all assets (from your assets directory). However, when a new file is added, it isn't copied to .tmp instantly, leading to the 404 s you mentioned.

To get over this, you can copy the file to the .tmp directory right after the upload finishes, before sending the response to the client.

This thread can help you with the copying.

A better way would be to upload directly to a CDN or S3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM