【问题标题】:Upload file asynchronously with Angular, ng-file-upload using a RESTful API使用 Angular 异步上传文件,使用 RESTful API ng-file-upload
【发布时间】:2015-05-04 20:27:38
【问题描述】:

我有一个表单,其中一些表单字段是文件上传。这就是我所拥有的:

  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!

【问题讨论】:

    标签: javascript php angularjs ng-file-upload


    【解决方案1】:

    好的,我终于明白了。

    我这样做了:

    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>
    

    由于没有人评论这种方法/技术,我将其视为世界上处理文件上传、角度和 REST API 的最佳方式。

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 2017-08-16
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多