如何使用angular-file-upload获取图像尺寸 [英] How to get the image dimension with angular-file-upload
问题描述
我当前正在使用 angular-file-upload 指令,并且我我几乎使用了演示中的确切代码.
I'm currently using the angular-file-upload directive, and I'm pretty much using the exact codes from the demo.
我需要在其中添加一个步骤来测试图像的尺寸,而我目前只能通过jQuery/javascript进行操作.
I need to add a step in there to test for the dimension of the image, and I am only currently able to do with via jQuery/javascript.
只是想知道在上传之前是否存在角度"检查图像尺寸的方法?
Just wondering if there's a "angular" way to check for the dimension of the image before it's being uploaded?
$scope.uploadImage = function($files) {
$scope.selectedFiles = [];
$scope.progress = [];
if ($scope.upload && $scope.upload.length > 0) {
for (var i = 0; i < $scope.upload.length; i++) {
if ($scope.upload[i] !== null) {
$scope.upload[i].abort();
}
}
}
$scope.upload = [];
$scope.uploadResult = [];
$scope.selectedFiles = $files;
$scope.dataUrls = [];
for ( var j = 0; j < $files.length; j++) {
var $file = $files[j];
if (/*$scope.fileReaderSupported && */$file.type.indexOf('image') > -1) {
var fileReader = new FileReader();
fileReader.readAsDataURL($files[j]);
var loadFile = function(fileReader, index) {
fileReader.onload = function(e) {
$timeout(function() {
$scope.dataUrls[index] = e.target.result;
//------Suggestions?-------//
$('#image-upload-landscape').on('load', function(){
console.log($(this).width());
});
//-------------------------//
});
};
}(fileReader, j);
}
$scope.progress[j] = -1;
if ($scope.uploadRightAway) {
$scope.start(j);
}
}
};
推荐答案
我认为您可以通过以下方式实现:
I think you can do it by:
var reader = new FileReader();
reader.onload = onLoadFile;
reader.readAsDataURL(filtItem._file);
function onLoadFile(event) {
var img = new Image();
img.src = event.target.result;
console.log(img.width, img.height)
}
这是从 https://github.com/nervgh/angular-file-upload/blob/master/examples/image-preview/directives.js .
我认为这更像是angularjs.但是,您可能需要对其进行修改以适合您的要求.
I think this is more angularjs. However, you may need to modify it to fit your requirement.
这篇关于如何使用angular-file-upload获取图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!