JavaScript 检查文件大小
是否可以继续使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
理论上,您可以使用 XHR 发出 HTTP
HEAD
请求并检查响应标头中的Content-Length
。一个
HEAD
请求与常规GET
请求相同除了服务器不得返回实际内容。换句话说,服务器会回复您尝试GET
资源时应有的标头,但随后会停止并且不发送文件。但是,一些服务器响应
HEAD< /code> 请求的
Content-Length
标头为0
,无论文件的实际大小如何。其他人则以文件的大小进行响应。为了实现这一点,您必须祈祷服务器将文件的实际大小返回给
HEAD
请求。如果是这样,获取该值很容易:
请注意,JSFiddle 的服务器始终返回
0< /code> 当我们
HEAD /
时,即使/
是16916
字节。另请注意 jQuery 的 文档所说的 关于 HTTP 请求类型选项的内容:
我刚刚在 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 theContent-Length
in the response headers.A
HEAD
request is identical to a regularGET
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 toGET
the resource, but then stops and does not send the file.However, some severs respond to a
HEAD
request with aContent-Length
header of0
, 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:
Note that JSFiddle's server always returns
0
when weHEAD /
, even though/
is16916
bytes.Also note what jQuery's docs say about the HTTP request type option:
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.Javascript 无法访问文件系统(谢天谢地)所以恐怕不能。
Javascript has no access to the filesystem (thankfully) so I'm afraid not.
josh3736 给出的想法很棒。只有他给出的代码示例拒绝在我的浏览器(Chrome 20、Firefox 13、IE 8、Opera 12)中工作,原因我不知道。
这是在我的情况下完美运行的一个:
我还想注意到,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:
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).IE:
如果您将“文档模式”设置为“标准”,您可以使用简单的 javascript“大小”方法来获取上传文件的大小。
将IE“文档模式”设置为“标准”:
然后,使用“大小”javascript方法获取上传文件的大小:
其他浏览器:
在所有其他浏览器上,您可以毫无问题地使用 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':
Than, use the 'size' javascript method to get the uploaded file's size:
Other browsers:
On all the other browsers you can use the javascript 'size' method without any problems.