Home  >  Article  >  Web Front-end  >  A simple example of using JavaScript to obtain the size of an uploaded file with specified precision

A simple example of using JavaScript to obtain the size of an uploaded file with specified precision

高洛峰
高洛峰Original
2016-12-09 10:22:531232browse

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>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn