phani
phani

Reputation: 131

upload file in sails js

I am new to sails js.In my code I am getting file from front-end but the file was showing error like below.Even my uploaded file is not saving in back-end folder.Once check my code .

upload: function(req, res) {
    if (req.method === 'GET')
        return res.json({ 'status': 'GET not allowed' });
    console.log("Get function is Excuted");

    var uploadFile = req.file('uploadFile');
    console.log(uploadFile);

    uploadFile.upload({ dirname: './assets/images' },function onUploadComplete(err, files) {


        if (err) {
            console.log(" Upload file is error");
            return res.serverError(err);

        }
        //  IF ERROR Return and send 500 error with error

     console.log(files);
        res.json({ status: 200, file: files });
    });
},

error code:

I am getting error in sails js console is this.

HTML code:

In my code I am facing a small problem please check it.Better to give any upload file example in sails js.

Upvotes: 1

Views: 4898

Answers (1)

Glen
Glen

Reputation: 1178

My best guess is you are not using the correct encoding type in your upload form. See more on that here https://www.w3schools.com/tags/att_form_enctype.asp

Here is an example of a simple form

<form action="/file/upload" enctype="multipart/form-data" method="post">
  <input type="file" name="file">
</form>

For completeness I have also included a basic working example of a sails file upload controller, which I have tested locally.

upload : function (req, res) {
  req.file('file').upload({
    // don't allow the total upload size to exceed ~100MB
    maxBytes: 100000000,
    // set the directory
    dirname: '../../assets/images'
  },function (err, uploadedFile) {
    // if error negotiate
    if (err) return res.negotiate(err);
    // logging the filename
    console.log(uploadedFile.filename);
    // send ok response
    return res.ok();
  }
}

Upvotes: 5

Related Questions