Eslam Soliman
Eslam Soliman

Reputation: 1296

Catch file uploaded size using Javascript event and cancel

I have a file up-loader. I do not want to check the size of the file on server side, I need some kind of listener to get the size uploaded each period and check if it is allowed or not using Javascript and I am using an asp.net file uploader.

Upvotes: 0

Views: 280

Answers (3)

Andrés Torres
Andrés Torres

Reputation: 733

Uploadify is a nice plugin for jQuery for uploading files, it has a pretty nice collection of callbacks on events, with full info about the file that is being uploaded and the progress of the upload.

Upvotes: 0

Yahia
Yahia

Reputation: 70369

AFAIK there is no javascript event to check client-side for a filesize when uploading... and since you don't want to check this server-side the answer is browser-dependent in the sense that it needs a client-side javascript API capable of what you want.

You could use HTML5+Javascript (File API) for this... but beware: there are borwsers/browser versions that don't supoport this... see details and samples:

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15861

i think you cannot fetch the file size detail from javascript, what you can do, you can access it from server side with the help of script block . like this.

protected void btnFileUpload1_Click(object sender, EventArgs e)
{
    int filesize; 
    filesize = FileUpload1.PostedFile.ContentLength; //to get the Size of uploaded files 
    if(filesize > yourMaxLength)
    {
           string errorScript = "<script type=\"text/javascript\">" +
         "alert('Invalid File, you cannot upload file exceeding ' + filesize );" +
                      "</script>";
         ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", errorScript);
    }
   else
   {
         //proceeed with upolad successful operation 
   }
}

Upvotes: 0

Related Questions