2015-09-05 153 views
0

我使用角度文件上傳將圖像和表單數據一起上傳到我的服務器。我一次能夠成功完成一幅圖像。使用角度文件上傳來上傳多個文件

我的問題是,在我的表單中,我需要上傳兩個圖像,使用單獨的輸入(以及發送其他表單數據)。我使用單獨輸入的推理是我的圖像之一是縮略圖圖像,另一圖像是英雄圖像。我需要能夠區分它們並將每個文件路徑插入到它們各自的數據庫列中。

我通過this github issue閱讀了解如何使用相同的輸入上傳多個文件,但我無法找到有關使用不同輸入上傳多個文件的任何內容。也許我誤解了他們。

現在,如果我嘗試選擇標題圖像,它只會更改縮略圖圖像的值。

這裏是我的形式:

<form> 
    ...(other text form inputs above)... 
    <div class="form-group"> 
     <label class="col-sm-2 control-label"> Teaser</label> 
     <div class="col-sm-8> 
     <input class="form-control" filestyle="" accept=".png,.gif,.jpg,.jpeg" data-button-text="Choose image" type='file' data-classbutton='btn btn-default' data-classinput="form-control inline" nv-file-select='' uploader='uploader' options="{'field': 'thumbnail_url'}" 
     </div> 
    </div> 
    <div class="form-group"> 
     <label class="col-sm-2 control-label"> Header Image</label> 
     <div class="col-sm-8> 
     <input class="form-control" filestyle="" accept=".png,.gif,.jpg,.jpeg" data-button-text="Choose image" type='file' data-classbutton='btn btn-default' data-classinput="form-control inline" nv-file-select='' uploader='uploader' options="{'field': 'header_url'}" 
     </div> 
    </div> 

</form> 

這裏是我如何將文件發送到我的服務器(在onCompleteAll回調我送我的表單數據到服務器):

$scope.uploader = new FileUploader({ 
     url: '/laravel/public/admin/events/thumbnail_url/file_upload', 
     alias: 'files', 
     headers: { 
     // 'X-CSRF-Token': n/a 
     }, 
     onBeforeUploadItem: function(item) { 
     /* Laravel file name alias */ 
     item.alias = 'file'; 
     }, 
     onSuccessItem: function(fileItem, response, status, headers) { 
     $scope.newEventForm[fileItem.field] = '/laravel/public/uploads/events/' + response.filename; 
     }, 
     onCompleteAll: function() { 
     /* Now ready to save form data */ 
     AdminEvent.save($scope.newEventForm).$promise.then(function(response) { 
      $scope.events.push(response.data); 
      toaster.pop('success', 'Added', 'Event added successfully.'); 
     }); 
     } 
    }); 

回答

0

我遇到了與ng-file-upload相同的問題,但我沒有使用angular-file-upload。

所以可能是這個解決方案對你有幫助。

在我的應用程序中,我有用戶配置文件圖像和背景圖像。

其中配置文件圖像輸入值我發送文件選項ng-file-upload但它沒有提供其他選項發送第二個文件輸入值。

來解決,我沒有什麼

  1. 創建第二個文件的輸入,並使用該輸入模型相同等,其中bg_avatar在鐵軌我的背景圖像屬性NG-模型=「user.bg_avatar」我的表屬性。

現在你可以獲得第二個文件輸入值與角度控制器中的用戶對象(user.bg_avatar),我們將發送到rails和當rails獲取它與其他用戶屬性保存。但是不要忘記允許這個屬性。

相關問題