一个项目需要在上传前判断图片的大小,我是通过下面的代码来实现
<html> <head> <title>获取上传图片大小</title> <script type = "text/javascript"> function GetFileSize() { var file = document.getElementById(‘fileToUpload‘).files[0]; if (file) { var fileSize = 0; if (file.size > 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + ‘MB‘; else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + ‘KB‘; } alert(fileSize); } </script> </head> <body> <input type="file" id="fileToUpload" value="" onchange="GetFileSize()"> </body> </html>
原文:http://www.cnblogs.com/maiguangyang/p/5133163.html