I am using a tutorial I found here to help me figure out file uploads in SailsJS. I have been able to get the uploads working when using the .ejs template engine in Sails, but I need the file uploads to work with the RESTapi. The URL I have setup is 'http://localhost:1337/file/upload', I am using Postman chrome app to to send a file to the server, but the response I get back is:
{
"status": 200,
"file": []
}
When not using the API (doing it within the .ejs template) I get the following response:
{
"status": 200,
"file": [
{
"fd": "path/to/uploaded/file/.tmp/uploads/assets/images/18c60ef7-b176-4375-8789-e0f80de29cea.pdf",
"size": 48541,
"type": "application/pdf",
"filename": "file.pdf",
"status": "bufferingOrWriting",
"field": "uploadFile"
}
]
}
I am not sure where the problem lies, am I not passing the file to the server? or is the server not dealing with the file correctly once it is received?
for reference, here is my controller code:
module.exports = {
upload: function (req, res) {
if(req.method === 'GET')
return res.json({'status':'GET not allowed'});
var uploadFile = req.file('uploadFile');
uploadFile.upload({ dirname: 'assets/images'},function onUploadComplete (err, files) {
if (err) return res.serverError(err);
res.json({status:200,file:files});
});
},
};
In the interests of closing this off so it might help somebody else, Postman was causing the problem - once i upgraded to the latest version everything worked as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With