1

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.

1

1 Answer 1

5

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();
  }
}
1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.