AngularJS 1.x:动态创建多个文件上载字段
对不起,我不常用角形的。我需要实现一个解决方案,在这个解决方案中,我可以从动态生成的输入字段上传文件 问题是我不能使用任何指令(这是因为我使用的框架实现了AngularJS) 我当前用于创建字段的代码:AngularJS 1.x:动态创建多个文件上载字段,angularjs,Angularjs,对不起,我不常用角形的。我需要实现一个解决方案,在这个解决方案中,我可以从动态生成的输入字段上传文件 问题是我不能使用任何指令(这是因为我使用的框架实现了AngularJS) 我当前用于创建字段的代码: $scope.addNewFile = function() { var newFileNo = $scope.files.length + 1; $scope.files.push({'id': 'file' + newFileNo}); }; $scope.removeFi
$scope.addNewFile = function() {
var newFileNo = $scope.files.length + 1;
$scope.files.push({'id': 'file' + newFileNo});
};
$scope.removeFile = function() {
var lastFile = $scope.files.length - 1;
$scope.files.splice(lastFile);
};
下面是上传文件的代码(不正确,因为静态id
和name
值:
$scope.uploadFile = function() {
var file = document.getElementById('file').files[0],
r = new FileReader;
r.onloadend = function (e) {
var data = e.target.result;
console.log('data');
console.log(data);
// http.post
};
r.readAsArrayBuffer(file);
};
这是我的HTML:
<div data-ng-repeat="file in files">
<div class="row top-buffer">
<div class="col-lg-10">
<input type="file"
ng-model="file.name"
name="file"
class="form-control"
id="file">
</div>
<div class="col-lg-2 text-right">
<div class="btn btn-primary" ng-click="uploadFile()">Upload</div>
</div>
</div>
</div>
<div class="row top-buffer">
<div class="col-lg-11"></div>
<div class="col-lg-1 text-right">
<div class="glyphicon glyphicon-plus" aria-hidden="true" ng-click="addNewFile()"></div>
<div class="glyphicon glyphicon-minus" aria-hidden="true" ng-click="removeFile()"></div>
</div>
</div>
上传
我真的不明白如何获得动态生成的文件字段的值…非常感谢您的帮助!没有
ng模型用于正确编写上载程序是一项非常困难的任务。上载组件/指令的一个主要问题是如何使其在Internet Explorer中工作;因为它只能在Internet Explorer上使用IE中的ce。另一个问题是,IE不会让您以编程方式“按下”按钮,它必须是真正的鼠标点击
AngularJs有上传库,只有google。但是,如果你想自己做,那么你可以使用my git repo作为例子来说明应该如何做。只需使用jwtUploader和jwtUploaderButton指令
Nong repeat
?为什么不能使用指令?这似乎是对一个或两个框架的错误使用。我理解。但这是由于另一个框架是如何构建的……所以我需要找到一个解决方案。不清楚您试图对文件做什么。您需要javascript中的实际文件内容吗?为什么不只是在发布到服务器时使用formData
?我需要获取文件以便$http.POST after:)。实际上,所有文件都是动态添加的。感谢您的解决方案,但正如我前面提到的,不幸的是,我无法使用自定义指令。
<div ng-repeat="file in files">
{{file.id}}:
<input type="file"
ng-attr-name="{{file.id}}"
ng-attr-id="{{file.id}}">
</div>
$scope.uploadFile = function() {
$scope.files.forEach(function(file) {
// $http.post: document.getElementById(file.id).files[0])
});
};