上传前获取文件大小
有没有办法在输入文件的更改事件中使用 AJAX / PHP 上传文件之前找出文件大小?
Is there any way to find out the file size before uploading the file using AJAX / PHP in change event of input file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
对于下面的 HTML,
请尝试以下操作:
请参阅以下线程:
如何使用 jQuery 检查文件输入大小?
For the HTML bellow
try the following:
See following thread:
How to check file input size with jQuery?
在 IE 和 FF 上工作
Work on IE and FF
这是一个在上传之前获取文件大小的简单示例。它使用 jQuery 来检测内容何时添加或更改,但您仍然可以在不使用 jQuery 的情况下获取
files[0].size
。Here's a simple example of getting the size of a file before uploading. It's using jQuery to detect whenever the contents are added or changed, but you can still get
files[0].size
without using jQuery.适用于所有浏览器的最佳解决方案;)
来自 http://www.dotnet-tricks.com/Tutorial/jquery/HHLN180712-Get-file-size-before-upload-using-jquery.html
更新:
我们将使用此函数来检查它是否是 IE 浏览器
Best solution working on all browsers ;)
from http://www.dotnet-tricks.com/Tutorial/jquery/HHLN180712-Get-file-size-before-upload-using-jquery.html
UPDATE :
We'll use this function to check if it's IE browser or not
我也遇到了同样的问题,看来我们还没有一个准确的解决方案。希望这可以帮助其他人。
经过一段时间的探索,我终于找到了答案。这是我使用 jQuery 获取文件附加的代码:
这只是获取文件大小的示例代码。如果您想做其他事情,请随意更改代码以满足您的需求。
I had the same problem and seems like we haven't had an accurate solution. Hope this can help other people.
After take time exploring around, I finally found the answer. This is my code to get file attach with jQuery:
This is just the example code for getting the file size. If you want do other stuffs, feel free to change the code to satisfy your needs.
支持 HTML5 的浏览器具有用于输入类型的 files 属性。
这当然不适用于较旧的 IE 版本。
Browsers with HTML5 support has files property for input type.
This will of course not work in older IE versions.
请不要使用
ActiveX
,因为它很可能会在 Internet Explorer 中显示可怕的警告消息并吓跑您的用户。如果有人想实现此检查,他们应该只依赖 FileList 对象在现代浏览器中可用,仅对旧版浏览器依赖服务器端检查 (渐进增强)。
Please do not use
ActiveX
as chances are that it will display a scary warning message in Internet Explorer and scare your users away.If anyone wants to implement this check, they should only rely on the FileList object available in modern browsers and rely on server side checks only for older browsers (progressive enhancement).
ucefkh 的解决方案效果最好,但由于 $.browser 在 jQuery 1.91 中已被弃用,因此必须更改为使用 navigator.userAgent:
ucefkh's solution worked best, but because $.browser was deprecated in jQuery 1.91, had to change to use navigator.userAgent:
您需要执行 ajax HEAD 请求来获取文件大小。使用 jquery 是这样的
you need to do an ajax HEAD request to get the filesize. with jquery it's something like this
通过
files.item(i).size获取文件的大小。
你应该尝试这个。
https://www.geeksforgeeks.org /使用-javascript-jquery上传时验证文件大小/
Get the size of the file by
files.item(i).size.
You should try this.
https://www.geeksforgeeks.org/validation-of-file-size-while-uploading-using-javascript-jquery/
您可以使用 PHP 文件大小函数。在使用 ajax 上传期间,请首先通过向 php 脚本发出 ajax 请求来检查文件大小,该脚本检查文件大小并返回值。
You can use PHP filesize function. During upload using ajax, please check the filesize first by making a request an ajax request to php script that checks the filesize and return the value.
您可以使用 HTML5 文件 API: http://www.html5rocks.com/en/ Tutorials/file/dndfiles/
但是,您应该始终为旧版浏览器提供 PHP(或您使用的任何其他后端语言)的后备方案。
You can by using HTML5 File API: http://www.html5rocks.com/en/tutorials/file/dndfiles/
However you should always have a fallback for PHP (or any other backend language you use) for older browsers.
就我个人而言,考虑到 HTML 标准,我认为 网络世界的答案 是当今最好的答案。如果您需要支持IE< 10、您将需要使用某种形式的ActiveX。我会避免涉及针对 Scripting.FileSystemObject 进行编码或直接实例化 ActiveX 的建议。
在这种情况下,我成功地使用了第 3 方 JS 库,例如 plupload ,它可以配置为使用 HTML5 api 或 Flash/Silverlight 控件来回填不支持这些的浏览器。 Plupload 有一个客户端 API,用于检查适用于 IE << 的文件大小。 10.
Personally, I would say Web World's answer is the best today, given HTML standards. If you need to support IE < 10, you will need to use some form of ActiveX. I would avoid the recommendations that involve coding against Scripting.FileSystemObject, or instantiating ActiveX directly.
In this case, I have had success using 3rd party JS libraries such as plupload which can be configured to use HTML5 apis or Flash/Silverlight controls to backfill browsers that don't support those. Plupload has a client side API for checking file size that works in IE < 10.