开发者

How to get file size before uploading?

Is there any possible way to get the size of a file before uploading it through the browser?开发者_StackOverflow

I found Check file size before uploading file, but the script doesn't work with Internet Explorer 6, 7 or 8. I have not tested with IE 9. I'm looking for a script which will work properly with any browser.


If you're willing to introduce a dependency on Flash, then you could use that to get the file size (and handle the upload too).

Flash supports file-selection via the FileReference class, which gives you access to some metadata including the file size.

With some help from the ExternalInterface class, you could call a JavaScript function and give it this information.

You could then upload the file from Flash instead of with the usual input element, if the file size is appropriate.

Check out YUI's uploader for a really easy-to-use and well-documented free component that handles all this for you (you only need to write a little JavaScript, and you can style the pre-built Flash component however you want).


I Add this answer to everyone after this can use:

function findSizeandalert() {
    var fileInput =  document.getElementById("fileupld");
    try{
        var fsize = fileInput.files[0].size; // Size returned in bytes.
        var fsizekb = fsize/1024;  // Size Convert to KBytes.

        if(fsizekb>100){
            alert("Your File size is more than specific size; Size of your file"+fsizekb.toFixed(2)+" KB");
            return false;
        }
    }catch(e){
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var e = objFSO.getFile( fileInput.value);
        var fileSize = e.size;
        var fsizekb = filesize/1024;

        if(fsizekb>100){
            alert("Your File size is more than specific size; Size of your file.(CC)"+fsizekb.toFixed(2)+" KB"); 
            return false;
        }
    }
    return true;
}

Then in your form at onsubmit events:

 <form method="post" enctype="multipart/form-data" name="frm_upload" onSubmit="return findSizeandalert()">
    <input id="fileupld"  type="file" name="fupl" accept="image/x-png,image/jpeg" >
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜