AsyncFileUpload restrict file size to upload

Mithir picture Mithir · Jun 13, 2011 · Viewed 8.9k times · Source

I am using AsyncFileUpload in order to allow users to asynchronously upload files.

I want to limit the size of the file to 1MB.

So far as to what I've seen I can only get the length of the file upon upload completion

like when upload starts:

(OnClientUploadStarted)

function UploadStarted(sender,args) 
{
   //if bigger than 1MB (approximately)
   if (args.get_length() > 1000000 ) 
   {
       ShowActionNotificationError( errorMessage); 
       return false;  
    }
}

args.get_length() is null , so I can't use it...

And when upload is completed:

(OnClientUploadComplete)

function UploadComplete(sender,args) 
{
    //if bigger than 1MB (approximately)
    if (args.get_length() > 1000000 ) 
    {
        ShowActionNotificationError( errorMessage); 
        return false;  
    }
}

works ok, but file was already uploaded...

So How can I know the size of the file before uploading it? am I missing something very simple?

I would really like to do it without handling HTTP Request length and the sort.

Thanks!

Answer

user3083726 picture user3083726 · Dec 9, 2013

You can do this in the client side Upload Start event.

if (sender._inputFile.files[0].size >= maxFileSize) {
    sender._stopLoad();
}

_stopLoad will call your Upload Error event.