7

我正在使用ng-flow在我的 AngularJS 应用程序中上传文件。我能够成功保存数据以及通过 ng-flow 上传多个文件。但是,在查询数据并通过 JSON 获取数据时,我不确定如何将文件添加到每一行的 ng-flow 对象中。每个文件都是用 JSON 字符串编码的 base 64。

为了澄清,我得到了每口井,每口井都有一个名称、位置、许可证等和多个图像。除图像外,井的所有属性都已成功填充到 DOM 中。

HTML:

...
<div flow-init flow-name="well.flow">
    <span class="btn" flow-btn flow-attrs="{accept:'image/*'}">Upload File</span>

    <table>
        <tr ng-repeat="file in well.flow.files">
            <td>{{ $index+1 }}</td>
            <td>{{ file.name }}</td>
            <td>{{ file.msg }}</td>
            <td><span ng-click="file.cancel()"><i class="icon-remove"></i></span></td>
        </tr>
    </table>
</div>

在 AngularJS 控制器内部:

wellsFactory.getData($scope.wellsParams).then(function(data){
        angular.forEach(data.wells, function(wells, wKey){

            if(wells.files){
                var list = [];
                angular.forEach(wells.files, function(files, fKey){

                    var binaryFile = atob(files.file);
                    var byteNumbers = new Array(binaryFile.length);
                    for (var i = 0; i < binaryFile.length; i++) {
                        byteNumbers[i] = binaryFile.charCodeAt(i);
                    }
                    var byteArray = new Uint8Array(byteNumbers);
                    var blob = new Blob([byteArray.buffer], {type: "image/png"});

                    list[fKey] = blob;
                });
            /* Add blob files to wells ng-flow  */
            data.wells[wKey].flow.files = list; /* breaks */
            //** How do I add ng-flow files? **//
            }
        });

        $scope.wells = data.wells;
    });

我已经成功测试了图像文件的输出 JSON base64 数据,即使在将它们设置为 blob 之后也是如此。

/* Test Each File (within foreach) */
...
var reader = new FileReader();
reader.onload = function(e){
    console.log(e.target.result);
};
reader.readAsDataURL(blob);
...

如何正确地将基于 blob 的图像文件加载到每行的 ng-flow 对象中?

4

2 回答 2

11

如果要向流中添加新文件,请使用现有方法flow.addFile(file)

var blob = new Blob(['a'], {type: "image/png"});
blob.name = 'file.png';
flow.addFile(blob);

如果要清除flow.files,请使用flow.cancel().

注意:flow.files不是 blob 数组,它是FlowFile https://github.com/flowjs/flow.js#flowfile数组

可以使用file属性 ( flow.files[0].file) 访问 Blob。

于 2014-06-03T18:49:41.223 回答
1

对于我的 rails 应用程序,在编辑操作中我想预加载现有图像。Listing_images 是我现有图像的 json 对象。调用 addFile 调用所有回调并尝试上传服务器上已经存在的文件。跳过回调并只搭建一些占位符:

  $scope.initExistingImages = function(listing_images, flowObj) {

    Flow.prototype.addExistingFile = function (file, event) {
      var f = new Flow.FlowFile(this, file);
      this.files.push(f);
    };

    angular.forEach(listing_images, function(value, key) {
      var blob = new Blob(['pre_existing_image'], {type: value.image_content_type});
      blob.name = value.image.image_file_name;
      blob.image_url = value.image.image_url;
      blob.image_id = value.image.id;
      blob.alt_text = value.alt_text;
      blob.listing_image_id = value.id;
      flowObj.addExistingFile(blob);
    });
  };
于 2015-08-11T21:04:59.637 回答