在sails js上传文件

时间:2017-05-02 12:03:11

标签: 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 });
    });
},

错误代码:

I am getting error in sails js console is this.

HTML code:

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

1 个答案:

答案 0 :(得分:1)

我最好的猜测是您没有在上传表单中使用正确的编码类型。点击此处了解详情https://www.w3schools.com/tags/att_form_enctype.asp

以下是简单表单的示例

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

为了完整性,我还提供了一个sails文件上传控制器的基本工作示例,我在本地进行了测试。

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();
  }
}
相关问题