我需要防止用户上传大图像到服务器,并希望使用简单的输入type=“文件”。使用下面的jQuery,当this.files超过1048000时,如何重置“输入文件”?
$('.image-file').bind('change', function() {
if ( this.files[0].size > 1048000 ) {
alert('Image size can not more than 1Mb.');
// going to reset the file
}
});
<input type="file" name="photo[]" class="image-file">
<input type="file" name="photo[]" class="image-file">
<input type="file" name="photo[]" class="image-file">发布于 2013-07-26 03:11:15
请尝试以下代码。但是要获得文件大小,可以使用服务器端代码,这将返回文件的大小。
$('#form_id').each(function(){
this.reset();
});
<input id="fileInput" type="file" onchange="AlertFilesize();" />下面的代码是使用javascript函数获取文件大小。
function AlertFilesize(){
if(window.ActiveXObject){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.getElementById('fileInput').value;
var thefile = fso.getFile(filepath);
var sizeinbytes = thefile.size;
}else{
var sizeinbytes = document.getElementById('fileInput').files[0].size;
}
var fSExt = new Array('Bytes', 'KB', 'MB', 'GB');
fSize = sizeinbytes; i=0;while(fSize>900){fSize/=1024;i++;}
alert((Math.round(fSize*100)/100)+' '+fSExt[i]);}
https://stackoverflow.com/questions/17872177
复制相似问题