在sails js中上传文件

pha*_*ani 1 sails.js waterline

我是sails js的新手。在我的代码中,我从前端获取文件,但文件显示如下错误。即使我上传的文件没有保存在后端文件夹中。一旦检查我的代码。

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 });
    });
},
Run Code Online (Sandbox Code Playgroud)

错误代码:

我在sails js控制台中收到错误是这个。

HTML代码:

在我的代码中,我遇到了一个小问题,请检查它。最好在sails js 中提供任何上传文件示例。

Gle*_*len 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>
Run Code Online (Sandbox Code Playgroud)

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();
  }
}
Run Code Online (Sandbox Code Playgroud)