开发者

Getting upload file size before upload

开发者 https://www.devze.com 2023-01-25 01:37 出处:网络
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 don开发者_StackOverflow中文版e on the cl

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 don开发者_StackOverflow中文版e on the client side with jQuery or JavaScript. Any ideas how?


This cannot be done in pure Javascript in current browsers.

Instead, you can use Uploadify, which uses Flash.

In non-IE browsers, you can also use HTML5 to read files on the client.


$("#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.


Take a look at this post:

  • http://forums.digitalpoint.com/showthread.php?t=6704


Javascript doesn't have the ability to check file sizes (or access the file system for that matter). You'll need to upload the file to get the size


I suggest you look at the HTML5 File API. This, combined with some JS might be able to help you. I only say might because I have not yet had a chance to browse at this part of the HTML5 standard.

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).

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. The file gets discarded if it is not within PHP's acceptable limits, but only after the file is completely uploaded.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消