[英]How to upload file in AngularJS?
Can not create a file upload using AngularJS. 无法使用AngularJS创建文件上传。 There is a form:
有一种形式:
<form ng-submit="getElem()" ng-repeat="elem in elems" class="update-form">
<label for="name">Elem name: {{elem.name}}</label>
<input type="file" id="file" name="file" />
<button type="button" ng-click="addImage()" class="btn btn-success">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
Add
</button>
</form>
And I have a method in AngularJS: 我在AngularJS中有一个方法:
$scope.addImage = function () {
$scope.data = 'none';
var f = document.getElementById('file').files[0],
r = new FileReader(),
currentElem = this.elem.id;
r.onloadend = function(e){
$scope.data = e.target.result;
var updateUrl = 'https://example/find' + currentElem + '/images';
$http.post(updateUrl, $scope.data)
.success(function (data, status, headers, config) {
console.info('Update image fine!');
});
}
r.readAsBinaryString(f);
After I click to "Add" Button, I have an error "TypeError: Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'." 单击“添加”按钮后,出现错误“ TypeError:无法在'FileReader'上执行'readAsBinaryString':参数1不是'Blob'类型。” Why this happened?
为什么会这样? And How I can fix this, because I need to upload file?
以及由于需要上传文件,我该如何解决?
Try below function 尝试以下功能
$scope.add = function(){
var f = document.getElementById('file').files[0],
r = new FileReader();
r.onloadend = function(e){
var data = e.target.result;
//send you binary data via $http or $resource or do anything else with it
}
r.readAsBinaryString(f);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.