当前位置:网站首页>JS format file unit display
JS format file unit display
2022-06-13 08:47:00 【fengyehongWorld】
Mode one
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js Format file unit display </title>
</head>
<body>
<input id="file" type="file" onchange="fileUpload(this)">
</body>
<script> // File long transfer callback function function fileUpload(obj) {
const fileObj = obj.files[0]; // Print the size of the long transmission file console.log(formatFileSize(fileObj.size)); } // Format file size function formatFileSize(size) {
if (!size || isNaN(Number(size))) {
return '-'; } let index = 0; let k = size; const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB']; if (size >= 1024) {
while (k > 1024) {
k = k / 1024; index++; } } return `${
(k).toFixed(2)}${
units[index]}`; } </script>
</html>
Mode two
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Calculate the size of the uploaded file </title>
</head>
<body>
<input id="upload" type="file" />
<div id="size"></div>
</body>
<script> document.querySelector("#upload").addEventListener("change", function({
target}) {
// Get the uploaded file object const uploadFile = target.files[0]; const sizeEle = document.querySelector("#size"); if(uploadFile.length === 0) {
sizeEle.innerHTML = ''; sizeEle.style.display = 'none'; } else {
// Format the file size sizeEle.innerHTML = formatFileSize(uploadFile.size); // Set visible sizeEle.style.display = 'block'; } }); // Format the size of the uploaded file , Display in reasonable units const formatFileSize = function(bytes) {
const sufixes = ['B', 'kB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return `${
(bytes / Math.pow(1024, i)).toFixed(2)} ${
sufixes[i]}`; }; </script>
</html>
边栏推荐
- Yarn package management tool
- Installing pytorch under Anaconda
- Sky background map, navigation page lovefanfan top
- Three methods to make the scroll bar of div automatically scroll to the bottom
- Process and scheduled task management
- Vscode define code block -- define cursor position
- d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range
- Buuctf web (VI)
- Custom exception class myexception
- regular expression
猜你喜欢

How does jupyter notebook directly output the values of multiple variables after running?

2021-04-16

Mapbox usage, including drawing, loading, modifying, deleting points and faces, displaying pop ups, etc

Docker installing MySQL local remote connection docker container MySQL
![【Pycharm踩坑记录】配置远程运行后提示,[Errno 2] No such file or directory](/img/71/92c06496a9d982294cac61595e0392.png)
【Pycharm踩坑记录】配置远程运行后提示,[Errno 2] No such file or directory

Screenshot of cesium implementation scenario

Cesium achieves sunny, rainy, foggy, snowy and other effects

DIY UAV (anonymous controller p2+f330 rack)

JS - set countdown for Date object case

Cesium displays a pop-up box at the specified position and moves with the map
随机推荐
Process and scheduled task management
Installing pytorch under Anaconda
ERP outlet
Animation through svg
Vscode double shortcut keys up, down, left and right
Tiktok keyword search list interface, ultra detailed interface docking steps
Screenshot of cesium implementation scenario
Basic use of cesium, including loading images, terrain, models, vector data, etc
Cesium achieves sunny, rainy, foggy, snowy and other effects
Yarn package management tool
d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range
Cesium displays a pop-up box at the specified position and moves with the map
JS gets the first month of the year, the second month to the last month, and the first day to the last day
H5 mobile terminal adaptation
Vscode define code block -- define cursor position
GBase 8a V95与V86压缩策略类比
Gbase 8A disk problems and Solutions
Batch package and download Alibaba OSS files
Object array de encapsulation
Explanation of JS event loop mechanism and asynchronous tasks