上传前如何检查图像大小(例如1MB) [英] How to check image size (ex. 1MB) before uploading
本文介绍了上传前如何检查图像大小(例如1MB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在发送表格之前,有什么方法可以检查图像尺寸吗?
Is there any way to check the image size before sending the form?
我正在使用jquery.form
I'm using jquery.form
js:
$(document).ready(function() {
var options = {
target: '#myform',
};
$('#myformbutton').click(function() {
$('#myform').ajaxSubmit(options);
return false;
});
});
html:
<form action="myaction" method="post" id="myform" enctype="multipart/form-data">
<label for="id_title">Title</label>
<input id="id_title" type="text" name="title" maxlength="255" />
<label for="id_image">Image</label>
<input type="file" name="image" id="id_image" />
<input type="button" id="myformbutton" value="Add!" />
</form>
推荐答案
我回答了自己的问题:
$(document).ready(function() {
$('#id_image').bind('change', function() {
if(this.files[0].size > 1000141){
$('#formerror').html('File is too big');
$('#myformbutton').hide();
}else{
$('#formerror').html(' ');
$('#myformbutton').show('slow');
}
});
});
和html:
<div id="formerror"></div>
<form action="myaction" method="post" id="myform" enctype="multipart/form-data">
<label for="id_title">Title</label>
<input id="id_title" type="text" name="title" maxlength="255" />
<label for="id_image">Image</label>
<input type="file" name="image" id="id_image" />
<input type="button" id="myformbutton" value="Add!" />
</form>
这有效.
这篇关于上传前如何检查图像大小(例如1MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文