Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SailsJS API file upload

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});
        });
    },
};
like image 721
mcneela86 Avatar asked Nov 09 '22 18:11

mcneela86


1 Answers

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.

like image 166
mcneela86 Avatar answered Dec 08 '22 01:12

mcneela86