Sails Js POST请求

4
我有一个html表单,可以向我的Sails应用程序中的/upload/upld发送POST请求。我的目标是有两个参数:要上传的照片和拍摄照片的位置。我的上传控制器应该将文件上传到具有位置参数值的目录中。
      <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
                            }
                    );
            });
    }
1个回答

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接