首页 > Web开发 > 详细

angularjs input上传图片前获取图片的Size

时间:2015-10-08 16:11:02      阅读:164      评论: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/kklldog/p/4861221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!