SailsJS API file upload

Viewed 1249

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});
        });
    },
};
2 Answers
Related