如何使用 JavaScript 验证文件的大小?
当在页面上单击上传按钮时...想要检查在文件上传控件中选择的文件大小,并在文件大小超过时停止上传按钮的回发...使用 jquery/javascript 执行此操作的方法是什么?
when upload button clicked on page... want to check filesize selected in fileupload control and stop postback of upload button if file size exceeds... what are the ways to do this using jquery/javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管这不是一种万无一失的方法,但某些浏览器会检查您是否有一个隐藏的输入元素指定最大文件大小,如下所示:
[编辑]似乎主要浏览器之一从未真正实现过这种类型的检查。只是希望他们做到了。
在这种情况下,您只能使用 flash、silverlight、java 或其他一些客户端插件来限制文件上传大小(客户端)。
Although it's hardly a foolproof way, some browser check if you have a hidden input element specifying the maximum filesize like so:
[edit]It seems that this type of checking was never actually implemented by one of the main browsers. It was merely hoped that they did.
In that case you can only limit the file upload size (clientside) with flash, silverlight, java or some other clientside plugin.
不能,必须使用服务器端 PHP 或 ASP[.NET] 来执行此操作。
如果可能的话,黑客将会窃取文件。
You can't, you must use server-side PHP or ASP[.NET] to do so.
If it is possible, crackers will be stealing files.
没有办法在纯 Javascript 解决方案中实现这一点,因为出于安全原因,Javascript 无法访问本地文件系统。做你想做的事情的唯一万无一失的方法是在服务器上。根据您使用的技术,您也许能够在请求仍在发送时拦截该请求,并根据
Content-length
标头终止该请求。There is no way to implement this in a pure Javascript solution, since for security reasons, Javascript doesn't have access to your local filesystem. The only fool-proof way to do what you want is on the server. Depending on the technology you use, you may be able to intercept the request while it's still being sent and kill it based on the
Content-length
header.