簡體   English   中英

使用RESTful API與Angular ng-file-upload異步上傳文件

[英]Upload file asynchronously with Angular, ng-file-upload using a RESTful API

我有一個表單,其中一些表單字段是文件上傳。 這就是我所擁有的:

  1. 用戶填寫表格
  2. 用戶選擇要提交的文件
  3. 用戶按下提交

現在,這就是我想要做的:

  1. 將表單發布到服務器,獲取ID
  2. 將文件一發布到服務器myresource / ID / fileone
  3. 將文件二發布到服務器myresource / ID / filetwo ...

¿如何以編程方式執行此文件上傳? (我使用的是角度承諾,因此順序請求沒有問題...)

這是我的代碼:

$scope.upload = function (files, url) {
      if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];
          Upload.upload({
            url: url,
            //fields: {'username': $scope.username},
            file: file
          }).progress(function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
          }).success(function (data, status, headers, config) {
            console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
          });
        }
      }
    };

我的html:

<input type="file" class="btn btn-danger" ng-file-select ng-model="files" ng-multiple="multiple"> Doit!

<input class="btn btn-danger" ng-file-select ng-model="files" ng-multiple="multiple">Doit too!

好的,所以我終於明白了。

我這樣做:

  1. 選擇文件並將文件存儲在ng-model中
  2. 驗證一些東西
  3. 提交主要模型(擁有我要上傳的文件的模型)
  4. 將文件上傳到端點/ api / myentity / {id} / resumee,/ api / myentity / {id} / dni和/ api / myentity / {id} / picture。 其中{id}是我在上一步中剛剛創建的實體的ID。

因此,這里的技巧是執行兩個請求,一個請求創建實體並檢索ID,第二個請求只是上傳文件。

代碼如下:

// This is called every time the user selects a file
$scope.selectedFile = function (files, doc) {
      if (doc === 'resumee') $scope.documents.resumee = files.pop();
      else if (doc === 'dni') $scope.documents.dni = files.pop();
      else if (doc === 'picture') $scope.documents.picture = files.pop();
};

// This is called when the user submits the form
$scope.upload = function (docname, url) {

      var file = $scope.stepThree.documents[docname];

      return Upload.upload({
        url: url,
        method: 'POST',
        headers: {'Content-Type': file.type},
        fileFormDataName: docname,
        data: file,
        file: file
      }).progress(function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
      });
    };

最后,標記是這樣的:

<div class="form-horizontal">
        <div class="form-group">
          <div class="col-sm-2">
            <label> Resumen Curricular </label>
          </div>
          <div class="col-sm-2">
            <button class="btn btn-danger" ngf-select ngf-change="selectFile($files, 'resumee')"> Seleccione</button>
            <p>{{stepThree.documents.resumee.name}}</p>
          </div>
        </div>
 </div>

由於沒有人評論這種方法/技術,因此我將其作為處理文件上傳,Angular和REST API的最佳方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM