JS检测上传图片大小

<input type="text" size="4" value="100" name="fileSizeLimit" id="fileSizeLimit"/> K
<input type="file" name="file1" id="file1" size="40" onchange="changeSrc(this)"/>
<img src="about:blank" id="fileChecker" style="display:none;">
   
<script type="text/javascript" defer>
var oFileChecker = document.getElementById("fileChecker");
   
function changeSrc(filePicker)
{
    oFileChecker.src = filePicker.value;
}
   
oFileChecker.onreadystatechange = function ()
{
    if (oFileChecker.readyState == "complete")
    {
        checkSize();
    }
}
   
function checkSize()
{
    var limit  = document.getElementById("fileSizeLimit").value * 1024;
    alert((Number(oFileChecker.fileSize)/1024).toFixed(2)+"k")
    if (oFileChecker.fileSize > limit)
    {  
        alert("too large");
    }
    else
    {
        alert("ok");
    }
}
</script> 

你可能感兴趣的:(js,职场,休闲,检测上传图片大小)