Adsence750x90

Tuesday, January 25, 2011

Limiting file size or getting file size of Upload file in ASP.Net

this is a small code snippet to limit file size on file upload

Asp.Net FileUpload control's FileBytes.Length returns file size in Bytes.
Here we want file size in MB. so we want to divide size by 1024*1024.

ie
1024 kilobytes = 1 megabyte (MB)
1 Kilobyte = 1024 bytes

protected void Button1_Click(object sender, EventArgs e)
    {
        //checking size 
        double fileSize =(double) FileUpload1.FileBytes.Length;
        double fileinMB = fileSize / (1024 * 1024);
        if (fileinMB > limit)
        {
            Response.Write("Size is limited to 2MB");
        }
        else
        {
        //upload file
            Response.Write("Uploaded");
        }
       
    }

No comments: