0

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.

Raffaele
  • 737
  • 7
  • 21

1 Answers1

0

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 404s 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.

Community
  • 1
  • 1
galactocalypse
  • 1,905
  • 1
  • 14
  • 29
  • First thanks for your answer. I checked with node's file system and the file exists already in destination folder, in another test I checked also the file size and it looks like the file was completed before to send the response to the client. Doesn't it means that the file is already in destination folder? – Raffaele Oct 28 '15 at 16:50
  • Was the file present inside the `.tmp` directory before the response was sent? I'd suggest trying again in a private window just to be sure. – galactocalypse Oct 29 '15 at 13:06
  • Maybe I was not clear, the file was present in the destination folder, not (only) in the .tmp folder. The variable `uploadedFiles[0].fd` is the path of the file in the folder that the server exposes and for this reason it should be ready to answer to a http get request. – Raffaele Oct 30 '15 at 15:45