Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 4 February 2015

Introduction 
In this article we will learn how to limiting the size of the uploading images to maximum 2 mb(2097152 bytes). If the size of the image will be more than 2 mb(Mega Byte)  then it will show message  "Please upload jpg/jpeg/png/gif image of up to 2mb size only" and restrict the uploading.

C#.NET Code to restrict user to upload images up to specific size


protected bool LimitImageSize()
    {
        int fileSize = FileUpload1.PostedFile.ContentLength;
        //Limit size to approx 2mb for image
        if ((fileSize > 0 & fileSize < 2097152))
        {
            return true;
        }
        else
        {
           Response.WriteFile("Please upload jpg/jpeg/png/gif image of upto 2mb size only");      
            return false;
        }
    }

Now call this function LimitImageSize() before uploading the image on upload or Submit button.

VB.NET Code to restrict user to upload images of up to specific size

Protected Function LimitImageSize() As Boolean
        Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
        'limit size to approx 2mb  for image
        If (fileSize > 0 And fileSize < 2097152) Then
            Return True
        Else
            Response.WriteFile("Please upload jpg/jpeg/png/gif image of upto 2mb size only")
            Return False
        End If
    End Function

Now call this function LimitImageSize() before uploading the image on upload or Submit button.

Categories: , ,

0 comments:

Post a Comment