Home >Web Front-end >JS Tutorial >A simple example of using JavaScript to obtain the size of an uploaded file with specified precision
js implements obtaining the size of uploaded files with specified precision, mainly using html and JavaScript. Use the browser to run the following code and follow the operation: select file->get the order of file size.
Source code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获得文件大小</title> </head> <body> <form type="testForm" id="test"> <input type="file" id="testFile"> <input type="button" id="tstBtn" value="获得文件大小" onclick="getSize();"/> </form> <script type="text/javascript"> function getSize(){ var elt=document.getElementById("testFile");//获得文件对象 var len=elt.files[0].size/1024;//获得文件大小并转化为KB单位,采用默认的精度 document.write("文件大小: "+len.toFixed(2)+"KB");//输出指定精度的文件大小,这里是精度是两位小数 } </script> </body> </html>