我正在使用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对象的每一行?
如果要添加新文件到流,请使用现有的方法flow.addFile(file)
。
var blob = new Blob(['a'], {type: "image/png"});
blob.name = 'file.png';
flow.addFile(blob);
如果要清除flow.files
,则使用flow.cancel()
。
注意:flow.files
不是blobs数组,它是FlowFile
数组https://github.com/flowjs/flow.js#flowfile
Blob可以通过file
属性(flow.files[0].file
)访问。
对于我的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);
});
};