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.