简体   繁体   English

Javascript检查文件大小

[英]Javascript check file size

Is it possible to keep checking with javascript if the filesize of a file on a webserver (eg http://www.mysite.com/myfile.js ) is larger than 0 bytes and if so return a true or false value? 如果Web服务器上文件的文件大小(例如http://www.mysite.com/myfile.js )大于0字节,是否可以继续使用javascript进行检查?如果是,则返回true或false值?

Thanks in advance! 提前致谢!

Theoretically, you could use XHR to issue a HTTP HEAD request and check the Content-Length in the response headers. 从理论上讲,您可以使用XHR发出HTTP HEAD请求并检查响应头中的Content-Length

A HEAD request is identical to a regular GET request except the server MUST NOT return the actual content. HEAD请求与常规GET请求相同,但服务器不得返回实际内容。 In other words, the server replies with the headers it would have had you tried to GET the resource, but then stops and does not send the file. 换句话说,服务器回复您尝试GET资源时的标头,但随后停止并且不发送文件。

However, some severs respond to a HEAD request with a Content-Length header of 0 , regardless of the actual size of the file. 但是,无论文件的实际大小如何, 某些服务器都会响应 Content-Length标头为0HEAD请求。 Others respond with the size of the file. 其他人回应文件的大小。

In order to accomplish this, you'll have to pray your server returns a file's actual size to a HEAD request. 为了实现这一点,您必须祈祷您的服务器将文件的实际大小返回给HEAD请求。

If it does, getting that value is easy : 如果是这样,那么获得该值很容易

$.ajax('/myfile.js', {
    type: 'HEAD',
    success: function(d,r,xhr) {
       fileSize = xhr.getResponseHeader('Content-Length');
    }
});

Note that JSFiddle's server always returns 0 when we HEAD / , even though / is 16916 bytes. 请注意,当我们HEAD /时,JSFiddle的服务器总是返回0 ,即使/16916字节。

Also note what jQuery's docs say about the HTTP request type option: 还要注意jQuery的文档对HTTP请求类型选项的说法

The type of request to make ("POST" or "GET"), default is "GET". 要求的类型(“POST”或“GET”),默认为“GET”。 Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers. 注意:此处也可以使用其他HTTP请求方法,例如PUT和DELETE,但并非所有浏览器都支持它们。

I just tested this Fiddle in IE 6-10, Firefox 3.6-7, Opera 9-11, and Chrome, and every single browser correctly issued the HEAD request, so I wouldn't worry about that vague incompatibility statement. 我刚刚在IE 6-10,Firefox 3.6-7,Opera 9-11和Chrome中测试了这个小提琴,并且每个浏览器都正确地发出了HEAD请求,因此我不担心这种模糊的不兼容性声明。 Of more concern is how your server responds. 更值得关注的是您的服务器如何响应。

Javascript无法访问文件系统(谢天谢地),所以我不敢。

The idea given by josh3736 is great. josh3736给出的想法很棒。 Only the code example he gave, refused to work in my browser (Chrome 20, Firefox 13, IE 8, Opera 12), for the reason, that I don't know. 只有他给出的代码示例,拒绝在我的浏览器(Chrome 20,Firefox 13,IE 8,Opera 12)中工作,原因是我不知道。

Here is the one, that worked perfectly in my case: 这是一个,在我的情况下完美的工作:

jQuery.ajax
({
    cache: false,
    type: 'HEAD',
    url: 'myfile.js',
    success: function(d,r,xhr){alert('File size is ' + xhr.getResponseHeader('Content-Length') + ' bytes.')},
    error: function(xhr, desc, er){alert('ERROR: "' + xhr.responseText + '"')}
});

I want also to notice, that Apache on-board XAMPP server works just fine with HEAD request, but of course, when run on localhost, such request is blocked by a browser with error message: "Origin localhost is not allowed by Access-Control-Allow-Origin" (example from Chrome). 我还要注意,Apache板载XAMPP服务器可以正常使用HEAD请求,但当然,当在localhost上运行时,浏览器会阻止此类请求,并显示错误消息:“Access-Control不允许使用Origin localhost -Allow-Origin“(来自Chrome的例子)。

IE: If you set the Ie 'Document Mode' to 'Standards' you can use the simple javascript 'size' method to get the uploaded file's size. IE:如果您将“文档模式”设置为“标准”,则可以使用简单的javascript“大小”方法来获取上传文件的大小。

Set the Ie 'Document Mode' to 'Standards': 将即“文档模式”设置为“标准”:

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

Than, use the 'size' javascript method to get the uploaded file's size: 比,使用'size'javascript方法获取上传文件的大小:

<script type="text/javascript">
    var uploadedFile = document.getElementById('imageUpload');
    var fileSize = uploadedFile.files[0].size;
    alert(fileSize); 
</script>

Other browsers: On all the other browsers you can use the javascript 'size' method without any problems. 其他浏览器:在所有其他浏览器上,您可以使用javascript'size'方法,没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM