2017-09-22 53 views
1

我在我的角度控制器驗證圖像是一個集大小和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 = []; 
}) 
+1

調試時ü得到什麼的寬度和高度? – Rahul

+0

@Rahul我得到1920 1080 ...有我的問題,我把驗證錯誤的方式..謝謝你 – Beep

+0

width = 1920和height = 1080?那麼你是在反向比較 'if(height == 1080 && width == 1920)'試試這個 – Rahul

回答

1

當你得到width = 1920 and height = 1080因此改變你的,如果條件:

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

這應該修復它。