在Anggular网络应用程序中,我有一个表单。我想更新文件,所以我使用lf-ng-md-file-input。这是简单的html:
<lf-ng-md-file-input lf-files='files' multiple> </lf-ng-md-file-input>
在我的控制器中,我这样做:
let formData = new FormData();
angular.forEach(this.$scope.files,function(obj){
formData.append('files[]', obj.lfFile);
});
对其进行调试,我看到obj.lfFile存在:
lastModified:1473694273813
lastModifiedDate:Mon Sep 12 2016 17:31:13 GMT+0200 (ora legale Europa occidentale)
name:"test.jpg"
size:39790
type:"image/jpeg"
webkitRelativePath:""
问题是它没有将文件追加到我的formData中。循环后它仍然为空。为什么?
最佳答案
我为此写过一条指令,也许会对您有所帮助:
指示:
angular.module('content')
.directive('parFiles', files);
/* @ngInject */
function files($parse, $timeout) {
var directive = {
link: link,
restrict: 'A'
};
return directive;
function link(scope, element, attr) {
var fn = $parse(attr['pirFiles']);
element.bind('change', function (evt) {
var files = [], fileList, i;
fileList = evt.target.files;
if (fileList != null) {
for (i = 0; i < fileList.length; i++) {
files.push(fileList.item(i));
}
}
$timeout(function () {
fn(scope, {
$files: files,
$event: evt
});
});
});
}
}
视图:
<input type="file"
class="input-list__item"
ng-disabled="dr.isFileUploaded"
par-files="dr.addFiles($files)"
required />
控制器:
function addFiles(files) {
vm.files = files;
}