2016-11-13 79 views
0

是否可以將文件對象轉換爲圖像對象?
我需要從文件(該文件是一個圖像)的寬度和高度,這是我的代碼:AngularJS - 將文件對象轉換爲圖像對象

視圖:

<button id="image_file" class="md-button md-raised md-primary" 
type="file" ngf-select="uploadFile($file)"> 
SELECT FILE 
</button> 

<md-input-container> 
<div class="raised"> 
<input id="image_file_name" type="text" ng-model="vm.file.name"></input> 
</div>  
</md-input-container> 

控制器:

app.controller('imageController', function($scope, fileService) { 

$scope.vm = {}; 

$('.intro').show(1000); 

$scope.uploadFile = function(file) { 
$scope.vm.file = ""; 
$scope.vm.file = file; //<---- for example here, how it should be done? 
    fileService.uploadFile($scope); 
} 

服務:

angular.module('app') 
.service('fileService', function ($http, validationService) { 

     this.uploadFile = function ($scope){ 
     if (validationService.isFileValidate($scope)) { 
      $scope.vm.acceptableFormat = true; 
     } else { 
      $scope.vm.acceptableFormat = false; 
     } 

    }; 

    }); 
+0

如何選擇文件?我的意思是''在哪裏? – Searching

+0

@查看它在'view'代碼頂部的按鈕下,我在這裏使用ng-file-upload – dante

+0

仍然不行嗎? – Searching

回答

0

圖書館似乎已經給你所需要的大部分。讀取文件/圖像的寬度和高度屬性。

//Add accept option to filter only images 
<button id="image_file" class="md-button md-raised md-primary" 
type="file" accept="image/*" ngf-select="uploadFile($file)"> 
SELECT FILE 
</button> 

控制器

$scope.uploadFile = function(file) { 
$scope.vm.file = ""; 
$scope.vm.file = file;  
// show all file properties 
console.log(file); // Show available properties 
console.log(file.$ngfWidth +','+ file.$ngfHeight) // Your width and height 
fileService.uploadFile($scope); 

}

你或許應該看看它的Features