【问题标题】:Image validation controller angular keeps giving image to large图像验证控制器角度不断给图像大
【发布时间】:2018-03-03 21:14:54
【问题描述】:

我在我的角度控制器中进行了验证,以验证图像是否为固定尺寸并且为 1080x1920。

但是我尝试上传的每张图片都会收到错误消息

上传图片出错,请确保分辨率为1080x1920px

即使我上传了正确尺寸的图片。如果有人能在我的代码中发现为什么会这样的错误,我会很伤心?

HTML

<div ng-controller="UpLoadImage">

<div ng-repeat="step in stepsModel">
    <img class="thumb" ng-src="{{step}}"/>
</div>

<label for="file">Select File</label>
<input ng-model="file" type='file' name='file' id='fileinput' base-sixty-four-input required onload='onLoad' maxsize='600'
       accept='image/*' onchange='angular.element(this).scope().imageUpload(this)'/>

{{uploadError}}

<button>Add to array</button>

JavaScript

   .controller('UpLoadImage', function ($scope, $http, $timeout) {

    $scope.imageUpload = function(){
        //
        // $scope.uploadBtn = false;
        $scope.errorActive = false;
        $scope.success = false;
        $scope.newImage = false;

        var reader = new FileReader();
        var input, file;
        if (!window.FileReader) {
            alert("The file API isn't supported on this browser yet.");
            return;
        }
        input = document.getElementById('fileinput');
        if (!input) {
            $scope.errorActive = true;
            $scope.uploadError = "Um, couldn't find the file input element.";
            $scope.$apply();
        }
        else if (!input.files) {
            $scope.errorActive = true;
            $scope.uploadError = "This browser doesn't seem to support the `files` property of file inputs.";
            $scope.$apply();
        }
        else if (!input.files[0]) {
            $scope.errorActive = true;
            $scope.uploadError = "Please select a file before clicking 'Upload'";
            $scope.$apply();
        }
        else {
            file = input.files[0];
            size = file.size;
            if(size < 650000){
                var fr = new FileReader;
                fr.onload = function(e){
                    var img = new Image;

                    img.onload = function(){
                        var width = img.width;
                        var height = img.height;
                        if(width == 1080 && height == 1920){
                            $scope.uploadError = '';
                            $scope.errorActive = false;
                            $scope.playingNow.background = e.target.result;
                            $scope.newImage = true;
                            $scope.uploadError = 'image uploaded';
                            $scope.$apply();
                            reader.onload = $scope.imageIsLoaded;
                            reader.readAsDataURL(element.files[0]);
                        }else{
                            $scope.errorActive = true;
                            $scope.uploadError = 'Error Uploading the image, make sure the resolution is 1080x1920px';
                            $scope.$apply();
                        }
                    };
                    img.src = fr.result;
                };
                fr.readAsDataURL(file);
            }else{
                $scope.errorActive = true;
                $scope.uploadError = 'max file size supported is 650Kb please compress your image';

            }
        }
        $scope.$apply();
    };

    $scope.imageIsLoaded = function (e) {
        $scope.$apply(function () {
            $scope.stepsModel.push(e.target.result);
        });

        $scope.onLoad = function (e, reader, file, fileList, fileOjects, fileObj) {

        };
    };

    $scope.stepsModel = [];
})

【问题讨论】:

  • 在调试时得到什么宽度和高度?
  • @Rahul 我得到 1920 1080 ...这是我的问题,我把验证放错了..谢谢男人
  • 宽度 = 1920 和高度 = 1080 ?然后你在反向比较if(height== 1080 &amp;&amp; width== 1920)试试这个
  • 好的.. 将此作为答案发布.. 请接受它:)
  • 是的,为什么不呢,谢谢兄弟

标签: javascript html angularjs validation


【解决方案1】:
if(width == "1080" && height == "1920"){

这应该可以解决它。

【讨论】:

    【解决方案2】:

    当您收到width = 1920 and height = 1080 时,请更改您的 if 条件:

    if(height=== 1080 && width=== 1920){
           // your code
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2012-10-21
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      相关资源
      最近更新 更多