Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncFileUpload restrict file size to upload

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!

like image 689
Mithir Avatar asked Jun 13 '11 07:06

Mithir


2 Answers

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.

like image 121
user3083726 Avatar answered Oct 01 '22 23:10

user3083726


After some substantial research I realize this cannot be done using AJAX.

As I look around at sites like yahoo mail and gmail, it's done by Flash.

in hotmail it's done with Silverlight.

there is a free (for now) flash upload control called Uploadify...

I am now working on integrating it.

if I'm wrong please correct me! :)

like image 22
Mithir Avatar answered Oct 01 '22 23:10

Mithir