2015-07-21 202 views
4

我有一个html表单,它会在我的sails应用程序中向POST请求/upload/upld。我的目标是有两个参数:要上传的照片和拍摄照片的位置。我的上传控制器应该将文件上传到具有location参数值的目录。Sails Js 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

我发现这个问题是我如何构建自己的状态。由于Sails使用Skipper来处理multipart/form-data,我必须在隐藏文本字段后移动文件输入。