【JavaScript】File 计算单位要用 1000 bytes 还是 1024 bytes?

前言

当使用者从浏览器上传档案后,可以取得修改时间、档案名称、档案大小...相关的资讯如下:

lastModified
Returns the last modified time of the file, in millisecond since the UNIX epoch (January 1st, 1970 at Midnight).name
Returns the name of the file referenced by the File object.webkitRelativePath
Returns the path the URL of the File is relative to.size
Returns the size of the file in bytes.type
Returns the MIME type of the file.
// 单一档案const File = () => {    const [file, setFile] = useState({});    console.log("file: ", file);    return (        <input type="file" multiple onChange={evt => setFile(evt.target.files[0])} />    )}; 
// 多个档案在 `<input>` 加上 multipleconst File = () => {    const [fileList, setFileList] = useState([]);    if (fileList) {        for (const file of fileList) {            console.log("file: ", file);        }    }    return (        <input type="file" multiple onChange={evt => setFile(evt.target.files)} />    )};

取得 file size 后,这该如何计算呢

搜寻资料时会看到 1GB = 1000 bytes,也有一说是 1024 bytes。

电脑相关的单位是二进制,因此一直是 1 Kilobyte = 1024 bytes,1 Megabyte = 1024 kilobytes,1 Gigabyte = 1024 megabytes,然而 "kilo" 意思是 "千" 的意思,也就代表 1000,就慢慢转变为 1 Kilobyte = 1000 byte...

在 1998 年由国际电工委员会(IEC)订定新的单位,用来取代过去的旧单位,因而有了 1 Kibibyte (KiB) = 1024 bytes, 1 Mebibyte (MiB) = 1024 KiB, 1 Gibibyte(GiB) = 1024 MiB,但在长期的使用习惯下,大部分的人可能还是会有 1KB = 1024 bytes 的误解。

十进位(SI)单位(bytes)1 Kilobyte(KB)1000 B1 Megabyte(MB)1000 KB1 Gigabyte(GB)1000 MB二进位(IEC)单位(bytes)1 Kibibyte(KiB)1024 B1 Mebibyte(MiB)1024 KiB1 Gibibyte(GiB)1024 MiB

而 Mac 和 Windows 系统的档案计算单位,分别是用 1000 bytes 和 1024 bytes,因此像是云端储存空间,就会需要针对不同的装置有不同的计算单位,简单且详细的说明可以参考此部影片。

参考来源:
https://developer.mozilla.org/en-US/docs/Web/API/File
https://www.quora.com/Is-1-GB-equal-to-1024-MB-or-1000-MB
https://zh.wikipedia.org/zh-tw/Mebibyte


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章