JavaScript 检查文件大小

发布于 2024-12-12 06:08:32 字数 188 浏览 0 评论 0原文

是否可以继续使用 javascript 检查网络服务器上文件的文件大小(例如 http://www. mysite.com/myfile.js) 大于 0 字节,如果是,则返回 true 或 false 值?

提前致谢!

Is it possible to keep checking with javascript if the filesize of a file on a webserver (e.g. http://www.mysite.com/myfile.js) is larger than 0 bytes and if so return a true or false value?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗忘曾经 2024-12-19 06:08:32

理论上,您可以使用 XHR 发出 HTTP HEAD 请求并检查响应标头中的 Content-Length

一个 HEAD 请求与常规 GET 请求相同除了服务器不得返回实际内容。换句话说,服务器会回复您尝试 GET 资源时应有的标头,但随后会停止并且不发送文件。

但是,一些服务器响应HEAD< /code> 请求的 Content-Length 标头为 0,无论文件的实际大小如何。其他人则以文件的大小进行响应。

为了实现这一点,您必须祈祷服务器将文件的实际大小返回给 HEAD 请求。

如果是这样,获取该值很容易

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

请注意,JSFiddle 的服务器始终返回 0< /code> 当我们 HEAD / 时,即使 /16916 字节。

另请注意 jQuery 的 文档所说的 关于 HTTP 请求类型选项的内容:

要发出的请求类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他HTTP请求方法,例如PUT和DELETE,但并非所有浏览器都支持它们。

我刚刚在 IE 6-10、Firefox 3.6-7、Opera 9-11 和 Chrome 中测试了这个 Fiddle,每个浏览器都正确发出了 HEAD 请求,所以我不会担心这个模糊的问题不兼容声明。更值得关注的是您的服务器如何响应。

Theoretically, you could use XHR to issue a HTTP HEAD request and check the Content-Length in the response headers.

A HEAD request is identical to a regular GET request except the server MUST NOT return the actual content. 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.

However, some severs respond to a HEAD request with a Content-Length header of 0, regardless of the actual size of the file. 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.

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.

Also note what jQuery's docs say about the HTTP request type option:

The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

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. Of more concern is how your server responds.

-黛色若梦 2024-12-19 06:08:32

Javascript 无法访问文件系统(谢天谢地)所以恐怕不能。

Javascript has no access to the filesystem (thankfully) so I'm afraid not.

时光暖心i 2024-12-19 06:08:32

josh3736 给出的想法很棒。只有他给出的代码示例拒绝在我的浏览器(Chrome 20、Firefox 13、IE 8、Opera 12)中工作,原因我不知道。

这是在我的情况下完美运行的一个:

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 + '"')}
});

我还想注意到,Apache 板载 XAMPP 服务器可以很好地处理 HEAD 请求,但是当然,当在本地主机上运行时,这样的请求被浏览器阻止并显示错误消息:“Access-Control-Allow-Origin 不允许 Origin localhost”(来自 Chrome 的示例)。

The idea given by josh3736 is great. 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.

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).

岛徒 2024-12-19 06:08:32

IE:
如果您将“文档模式”设置为“标准”,您可以使用简单的 javascript“大小”方法来获取上传文件的大小。

将IE“文档模式”设置为“标准”:

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

然后,使用“大小”javascript方法获取上传文件的大小:

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

其他浏览器:
在所有其他浏览器上,您可以毫无问题地使用 javascript 'size' 方法。

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.

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:

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文