我正在使用ng-file-upload为我的angular项目,这是html代码:
<div class="form-group">
<button class="btn btn-warning" ngf-select="imagesSelected($files)" multiple="multiple" ngf-pattern="'image/*'" accept="image/jpeg,image/png">选择图片</button>
</div>
这是我的javascript代码:
$scope.imagesSelected = function (files) {
if (files.length > 0) {
angular.forEach(files, function (imageFile, index) {
$scope.upload = Upload.upload({
url: '/upload_image',
method: 'POST',
file: imageFile,
data: {
'fileName': imageFile.name
}
}).success(function (response, status, headers, config) {
...
});
});
}
};
第一次点击按钮选择图像文件,选择后立即上传图像文件,这是我所期望的。但是当我第二次单击上传按钮时,控制台有这个错误指向if文件。长度> 0)行:
Uncaught TypeError: Cannot read property 'length' of null
和文件选择窗口永远不会显示,第三次我点击上传按钮,它再次工作正常,第四次不,等等…ng-file-upload的版本是9.0.4,是lib的这个bug没有被修复还是我犯了一些错误?谢谢。
你写代码的方式是错误的。
$scope.imagesSelected = function (files) {
$scope.files = files;
if (files && files.length) {
Upload.upload({
url: '/upload_image',
method: 'POST',
data: {
files: files
}
}).then(function (response) {
$timeout(function () {
$scope.result = response.data;
});
}, function (response) {
if (response.status > 0) {
$scope.errorMsg = response.status + ': ' + response.data;
}
}, function (evt) {
$scope.progress =
Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
};
看到文档