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

为了澄清,我得到了每口井,每口井都有名称、位置、许可证等。和多个图像。除图像外,井的所有属性均已成功填充到 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 对象中?

有帮助吗?

解决方案

如果要向流程添加新文件,请使用现有方法 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

可以通过以下方式访问 Blob file 财产 (flow.files[0].file).

其他提示

对于我的rails应用程序,在编辑操作上,我想预示我现有的图像。listind_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);
    });
  };
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top