如何确定JavaScript中的文件大小? [英] How can you determine the file size in JavaScript?

查看:151
本文介绍了如何确定JavaScript中的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我帮助在线上一个论坛,在这个论坛上我们限制签名的大小。目前我通过我写的简单的Greasemonkey脚本来测试这个脚本。我们使用< div> 包装所有签名,脚本会查找它们,然后测量div的高度和宽度。

I help moderate a forum online, and on this forum we restrict the size of signatures. At the moment we test this via a simple Greasemonkey script I wrote; we wrap all signatures with a <div>, the script looks for them, and then measures the div's height and width.

所有的脚本现在都是确保签名位于特定的高度/宽度。我想自动开始测量签名内的图像的文件大小,以便脚本可以自动标记在签名中包含巨大图像的用户。但是,我似乎找不到一种方法来测量页面上加载的图像的大小。我搜索并找到一个特殊的IE(element.fileSize)的属性,但我显然不能在我的Greasemonkey脚本中使用。

All the script does right now is make sure the signature resides in a particular height/width. I would like to start measuring the file size of the images inside of a signature automatically so that the script can automatically flag users who are including huge images in their signature. However, I can't seem to find a way to measure the size of images loaded on the page. I've searched and found a property special to IE (element.fileSize) but I obviously can't use that in my Greasemonkey script.

有没有办法找到通过JavaScript在Firefox中显示图片的文件大小?

Is there a way to find out the file size of an image in Firefox via JavaScript?

编辑:人们误解了问题。论坛本身不主持图片;我们托管人们输入的BBCode作为他们的签名。所以,例如,人们输入:

People are misinterpreting the problem. The forums themselves do not host images; we host the BBCode that people enter as their signature. So, for example, people enter this:

This is my signature, check out my [url=http://google.com]awesome website[/url]!
This image is cool!  [img]http://image.gif[/img]

我想要能够检查通过Greasemonkey在这些图像上。我可以编写一个批处理脚本来扫描所有这些代码,但我只是想知道是否有办法增加我当前的脚本。

I want to be able to check on these images via Greasemonkey. I could write a batch script to scan all of these instead, but I'm just wondering if there's a way to augment my current script.

推荐答案

简短的答案,你不能

另外,检查jGuru 如何从具有输入文件类型的表单中查看JavaScript中的文件大小?

Also, Check on jGuru How can you check the file size from JavaScript in a form with an input type of file?

您会找到一些重要的一点

You will find some important points


答案很简单,你不能。

Well the answer is very simple, you cannot.

原因:浏览器的安全性不允许脚本
(Javascript / VBScript),甚至applet和ActiveX控件从本地硬盘读取
文件。如果您的代码
由某个证书颁发机构(CA)签名,您只能读取文件。现在输入类型
FILE也没有读取文件的权限。我们不能做任何关于这一点的
,因为这是HTML所说的。所以因为它不能
读取文件,它找不到输入
标签与之关联的文件的大小。由于找不到文件大小,所以没有
函数通过JavaScript / VBScript公开来返回文件大小。但是如果您需要找到文件大小
,则为了限制上传到Web服务器的文件的大小
。然后,您可以通过
对服务器端的文件内容进行计数,一旦用户将
提交给服务器。这是许多免费的电子邮件提供商,如
www.hotmail.com。

Reason: The browser security does not allow the scripts (Javascript/VBScript) or even applets and ActiveX Controls to read files from the local hard disk. You can only read files if your code is signed by some Certificate Authority (CA). Now the input type "FILE" also does not have the permission to read files. We cannot do anything about that since thats what HTML says. So since it cannot read files, it cannot find the size of the file with which the input tag is associated. Since it cannot find the file size, there is no function exposed by JavaScript/VBScript to return the file size. But if you need to find the file size, say in order to restrict the size of the file uploaded to your web-server. Then you can do so by counting the file contents on the server-side, once the user submits it to the server. Thats what many of the free e-mail providers like www.hotmail.com do.

这篇关于如何确定JavaScript中的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆