我使用 danialfarid 库文件上传。
https://github.com/danialfarid/angular-file-upload 我在“GitHub页面上的使用文章”中做同样的事情,但是在我选择文件后出现错误:“未捕获的类型错误:无法读取属性'长度' of undefined”,这个 undefined 是 $files。
这是我的控制器:
$scope.onFileSelect = function($files) {
console.log($files); // undefined
//$files: an array of files selected, each file has name, size, and type.
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
$scope.upload = $upload.upload({
url: '/cards/avatar/save_from_disk', //upload.php script, node.js route, or servlet url
data: {myObj: $scope.myModelObj},
file: file,
}).progress(function(evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function(data, status, headers, config) {
// file is uploaded successfully
console.log(data);
});
}
};
在我看来:
<input type="file" title="" accept="image/*" ng-file-select="onFileSelect($files)" class="upload" />