简体   繁体   English

上传前获取上传文件大小

[英]Getting upload file size before upload

When the user selects a file to be uploaded, is there a way I can get the exact size of this file before the upload even begins? 当用户选择要上传的文件时,有没有办法在上传开始之前获得该文件的确切大小? I'm guessing this needs to be done on the client side with jQuery or JavaScript. 我猜这需要在客户端使用jQuery或JavaScript完成。 Any ideas how? 有什么想法?

This cannot be done in pure Javascript in current browsers. 在当前浏览器中,这不能在纯Javascript中完成。

Instead, you can use Uploadify , which uses Flash. 相反,您可以使用使用Flash的Uploadify

In non-IE browsers , you can also use HTML5 to read files on the client . 在非IE浏览器中 ,您还可以使用HTML5读取客户端上的文件

$("#file_input_selector").bind('change', function(){
 alert(this.files[0].size);
});

Not sure of all the compatibility issues, but this seems to be working just fine for me. 不确定所有的兼容性问题,但这似乎对我来说很好。

Javascript doesn't have the ability to check file sizes (or access the file system for that matter). Javascript无法检查文件大小(或访问文件系统)。 You'll need to upload the file to get the size 您需要上传文件才能获得大小

I suggest you look at the HTML5 File API. 我建议你看看HTML5 File API。 This, combined with some JS might be able to help you. 这个,结合一些JS 可能能够帮助你。 I only say might because I have not yet had a chance to browse at this part of the HTML5 standard. 我只是说因为我还没有机会浏览HTML5标准的这一部分。

http://www.w3.org/TR/FileAPI/#dfn-filereader http://www.w3.org/TR/FileAPI/#dfn-filereader

The way PHP file uploads work, it is very hard to check file details before, or during a file upload (since the file is uploaded before your code even gets loaded). PHP文件上传的方式很难在文件上传之前或文件上传期间检查文件详细信息(因为文件是在代码加载之前上传的)。

I know it is possible to do some fancy things in some other languages (possibly Perl or Python) that handle the file uploading directly with the script (where the script opens the socket and handles the whole transfer itself), however PHP does this for you and accepts any file on your script's behalf. 我知道可以在其他语言(可能是Perl或Python)中做一些奇特的事情来处理文件直接上传脚本(脚本打开套接字并处理整个传输本身),但PHP会为你做这件事并代表您的脚本接受任何文件。 The file gets discarded if it is not within PHP's acceptable limits, but only after the file is completely uploaded. 如果文件不在PHP的可接受限制范围内,则文件将被丢弃,但仅在文件完全上载后才会被丢弃。

There have also been several file upload implementations made using Flash, but not being an ActionScript coder, I can't really help too much there either. 还有一些使用Flash制作的文件上传实现,但不是ActionScript编码器,我也不能真正帮助太多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM