加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何使用angular-file-upload获取图像尺寸

发布时间:2020-12-17 17:40:52 所属栏目:安全 来源:网络整理
导读:我目前正在使用 angular-file-upload指令,而且我几乎使用了演示中的确切代码. 我需要在那里添加一个步骤来测试图像的尺寸,我目前只能通过jQuery / javascript来做. 只是想知道在上传之前是否有一种“有角度”的方式来检查图像的尺寸? $scope.uploadImage =
我目前正在使用 angular-file-upload指令,而且我几乎使用了演示中的确切代码.

我需要在那里添加一个步骤来测试图像的尺寸,我目前只能通过jQuery / javascript来做.

只是想知道在上传之前是否有一种“有角度”的方式来检查图像的尺寸?

$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);
        }

    }
};

解决方法

我想你可以做到:

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.但是,您可能需要对其进行修改以满足您的要求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读