Javascript 使用angular js上传文件时如何对图像大小进行验证
我正在研究角度应用。我使用Javascript 使用angular js上传文件时如何对图像大小进行验证,javascript,angularjs,file-upload,Javascript,Angularjs,File Upload,我正在研究角度应用。我使用上传图像。这是我的HTML代码 <ul class="enlarge col-lg-9 margin_top10"> <li ng-repeat="image in images"> <img id="pic1" src="{{image.photoPath}}" ng-click="readImageUrl();" style="height: 40px; w
上传图像。这是我的HTML代码
<ul class="enlarge col-lg-9 margin_top10">
<li ng-repeat="image in images">
<img id="pic1"
src="{{image.photoPath}}" ng-click="readImageUrl();"
style="height: 40px; width: 50px;"/>
<span>
<img src="{{image.photoPath}}"
style="height: 200px; width: 400px;" id="pic2"/>
</span>
<a href="javascript:void(0)" ng-click="deletePhoto(image.photoPath,$index)" title="Delete">×</a>
</li>
<li id="selectedPhotos" style="width:700px;height:100px;float:left;"></li>
</ul>
图像将成功上载并保存。但我想给验证像用户不能上传图像大小超过1 MB。我不知道怎么做。请分享你的想法。提前谢谢 理想情况下,您可以在服务器上进行验证。这是一种选择吗?
$scope.readImageUrl = function() {
try {
var input = document.getElementById("fileSel");
if (input.files && input.files[0]) {
/* if(input.files.length==1){
$("#demo").show();
} */
var xhr = new XMLHttpRequest();
xhr.open('GET', input.files[0], true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
localSrc = URL
.createObjectURL(this.response);
};
xhr.send();
var reader = new FileReader();
reader.onload = function(e) {
$('#profilePic1').attr('src',
e.target.result);
$('#profilePic2').attr('src',
e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
} catch (ex) {
console.log(ex);
}
};