2016-11-13 80 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