Home > Web Front-end > JS Tutorial > 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

高洛峰
Release: 2016-12-09 10:22:53
Original
1333 people have browsed it

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>
Copy after login


Related labels:
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template