AngularJS - 将文件对象转换为图像对象

时间:2016-11-13 20:12:32

标签: javascript 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;
        }

    };

     });

1 个答案:

答案 0 :(得分: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