码迷,mamicode.com
首页 > Web开发 > 详细

关于angularjs input上传图片前获取图片的Size 浅析

时间:2015-10-09 00:25:50      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

首先我们需要一个指令来追踪input的change。ngChage不适用input[file]。

app.directive("fileread", [function () {
return {
scope: {
selectedFile: "=",
changed: ‘&‘
},
link: function(scope, element, attributes) {
element.bind("change", function(changeEvent) {
scope.$apply(function() {
scope.selectedFile = changeEvent.target.files[0];
// or all selected files:
// scope.fileread = changeEvent.target.files;
console.log(‘file selected.‘);
if (scope.changed()) {
scope.changed()(scope.selectedFile);
}
});
});
}
};
}]);

然后在controller里定义file的变量跟change绑定的function。

$scope.showFileSelectBox = function () {
$("#imgSelectInput").click();
};

$scope.imageSelected = function(file) {
var image;

if (file) {

image = new Image();

image.onload = function () {

$scope.editObj.Width = this.width;
$scope.editObj.Height = this.height;
};

image.src = $window.URL.createObjectURL(file);

}
};

然后是html

<button type="button"  ng-click="showFileSelectBox()">上传</button>
<input type="file" style="display: none" accept="image/*" fileread selectedfile="selectedImgFile" id="imgSelectInput" changed="imageSelected" />

关于angularjs input上传图片前获取图片的Size 浅析

标签:

原文地址:http://www.cnblogs.com/wangjie-01/p/4862620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!