【问题标题】:Sails Js POST requestSails Js POST 请求
【发布时间】:2015-07-21 06:31:39
【问题描述】:

我有一个 html 表单,它在我的sails 应用程序中向/upload/upld 发出 POST 请求。我的目标是有两个参数:要上传的照片和拍摄照片的位置。我的上传控制器应该使用 location 参数的值将文件上传到目录。

      <form method="post" action="/upload/upld" enctype="multipart/form-data">
        <span class="btn btn-default btn-file wow bounceIn top-buffer">
          Browse <input type="file" name="photo">
          <input type="hidden" name="location" value="istana" />
        </span>
          <input type="submit" class="btn btn-primary wow bounceIn top-buffer">
      </form>

不幸的是,这段代码似乎不起作用。每当我只上传一个文件时,控制台上的输出都会显示以下内容。我不确定为什么 upld 方法似乎在一次上传时运行了两次。

{ location: 'istana', id: undefined }
istana
{ id: undefined }
undefined

我的上传控制器如下所示:

    upld: function (req, res) {
            var params = req.params.all();
            console.log(params);
            var this_location = params.location;
            console.log(this_location);
            req.file('photo').upload({ dirname:require('path').join('./',this_location)},function (err, files) {
                    if (err){
                            return res.serverError(err);
                    }
                    return res.view('homepage',{

                                    message: files.length + ' file(s) uploaded successfully!',
                                    files: files
                            }
                    );
            });
    }

【问题讨论】:

    标签: javascript express sails.js


    【解决方案1】:

    我发现问题在于我的表单结构。由于 Sails 使用 Skipper 处理 multipart/form-data 的方式,我不得不将文件输入移到隐藏文本字段之后。

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 2015-09-07
      • 1970-01-01
      • 2019-11-04
      • 2015-11-11
      • 2016-08-05
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      相关资源
      最近更新 更多