2015-07-21 113 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,我必須在隱藏文本字段後移動文件輸入。