Javascript 在javascript中获取文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2966076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting file size in javascript
提问by scatman
given a path of a file like:
C:\file.jpg
how can i get the size of the file in javascript?
给定文件的路径,例如:
C:\file.jpg
如何在 javascript 中获取文件的大小?
回答by Andy E
If it's not a local application powered by JavaScript with full access permissions, you can't get the size of any file just from the path name. Web pages running javascript do not have access to the local filesystem for security reasons.
如果它不是由具有完全访问权限的 JavaScript 提供支持的本地应用程序,则无法仅从路径名获取任何文件的大小。出于安全原因,运行 javascript 的网页无法访问本地文件系统。
You can use a graceful degrading file uploader like SWFUploadif you want to show a progress bar. HTML5 also has the File API, but that is not widely supported just yet. If a user selects the file for an input[type=file]element, you can get details about the file from the filescollection:
如果您想显示进度条,您可以使用优雅的降级文件上传器,如SWFUpload。HTML5 也有File API,但目前还没有得到广泛支持。如果用户为某个input[type=file]元素选择文件,您可以从files集合中获取有关该文件的详细信息:
alert(myInp.files[0].size);
回答by Veer
function findSize() {
var fileInput = document.getElementById("fUpload");
try{
alert(fileInput.files[0].size); // Size returned in bytes.
}catch(e){
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var e = objFSO.getFile( fileInput.value);
var fileSize = e.size;
alert(fileSize);
}
}
回答by Sarfraz
If you could access the file system of a user with javascript, image the bad that could happen.
如果您可以使用 javascript 访问用户的文件系统,请想象可能发生的坏事。
However, you can use File System Object but this will work only in IE:
但是,您可以使用文件系统对象,但这仅适用于 IE:
http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript
http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript
回答by VoteyDisciple
You cannot.
你不能。
JavaScript cannot access files on the local computer for security reasons, even to check their size.
出于安全原因,JavaScript 无法访问本地计算机上的文件,甚至无法检查它们的大小。
The only thing you can do is use JavaScript to submit the form with the filefield to a server-side script, which can then measure its size and return it.
您唯一能做的就是使用 JavaScript 将带有file字段的表单提交给服务器端脚本,然后服务器端脚本可以测量其大小并返回它。
回答by Jaumzera
Try this one.
试试这个。
function showFileSize() {
let file = document.getElementById("file").files[0];
if(file) {
alert(file.size + " in bytes");
} else {
alert("select a file... duh");
}
}
<input type="file" id="file"/>
<button onclick="showFileSize()">show file size</button>
回答by Stev
To get the file size of pages on the web I built a javascript bookmarklet to do the trick. It alerts the size in kb's. Change the alert to a prompt if you want to copy the filesize.
为了获取网络页面的文件大小,我构建了一个 javascript 书签来实现这一点。它以 kb 为单位提醒大小。如果要复制文件大小,请将警报更改为提示。
Here's the bookmarklet code for the alert.
这是警报的书签代码。
<a href="javascript:a=document.getElementsByTagName('HTML')[0].outerHTML;b=a.length/1024;c=Math.round(b);alert(c+' kb');">Doc Size</a>
Here's the bookmarklet code for the prompt.
这是提示的书签代码。
<a href="javascript:a=document.getElementsByTagName('HTML')[0].outerHTML;b=a.length/1024;c=Math.round(b);prompt('Page Size',c+' kb');">Doc Size</a>
See it in action at http://bookmarklets.to.g0.to/filesize.php
回答by baloo
You can't get the file size of local files with javascript in a standard way using a web browser.
您无法使用 Web 浏览器以标准方式使用 javascript 获取本地文件的文件大小。
But if the file is accessible from a remote path, you might be able to send a HEAD request using Javascript, and read the Content-length header, depending on the webserver
但是,如果可以从远程路径访问该文件,则您可以使用 Javascript 发送 HEAD 请求,并读取 Content-length 标头,具体取决于网络服务器

