Validate and upload image files in asp.net

IntroductionIn previous articles i explained How to resize image in Asp.net and How to create thumbnail, small and large version of the uploaded image in Asp.net.  It is always better to validate the file extension before uploading file i.e. you want that user can upload only the files that you want e.g. if you want the user can upload only .jpg, .jpeg, .png, .gif , .bmp files then you have to write the code that can check the extension of the file the user is going to upload. Here is the way:
  • Place a FileUpload control and a button control on design page(.aspx)
<asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUploadImage" runat="server" Text="upload Image"
            onclick="btnUploadImage_Click"  />

C#.Net Code to Validate and upload image files in asp.net
  • In the code behind (.aspx.cs) file write the code:
    private bool IsValidExtension(string filePath)
    {
        bool isValid = false;
        string[] fileExtensions = {".bmp",".jpg",".png",".gif",".jpeg",".BMP",".JPG",".PNG",".GIF",".JPEG"};

        for (int i = 0; i <= fileExtensions.Length - 1; i++)
        {
            if (filePath.Contains(fileExtensions[i]))
            {
                isValid = true;
            }
        }
        return isValid;
    }

    protected void btnUploadImage_Click(object sender, EventArgs e)
    {
        if (IsValidExtension(FileUpload1.PostedFile.FileName))
        {
            //write code to upload file
            string filePath = (Server.MapPath("Uploads/") + Guid.NewGuid() + FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(filePath);
        }
        else
        {
            Response.Write("Please upload .jpeg,.png,.gif,.jpg,.bmp image only");
        }
    }


VB.Net Code to Validate and upload image files in asp.net
  • In the code behind (.aspx.vb) file write the code:
Private Function IsValidExtension(ByVal filePath As String) As Boolean
        Dim isValid As Boolean = False
        Dim fileExtensions As String() = {".bmp", ".jpg", ".png", ".gif", ".jpeg", ".BMP", _
         ".JPG", ".PNG", ".GIF", ".JPEG"}

        For i As Integer = 0 To fileExtensions.Length - 1
            If filePath.Contains(fileExtensions(i)) Then
                isValid = True
            End If
        Next
        Return isValid
    End Function

    Protected Sub btnUploadImage_Click(ByVal sender As Object, ByVal e As EventArgs)
        If IsValidExtension(FileUpload1.PostedFile.FileName) Then
            'write code to upload file
            Dim filePath As String = (Server.MapPath("Uploads/") + Guid.NewGuid() + FileUpload1.PostedFile.FileName)
            FileUpload1.SaveAs(filePath)
        Else
            Response.Write("Please upload .jpeg, .png, .gif, .jpg, .bmp image only")
        End If
    End Sub

 Note: In this example Uploaded file will be saved in the Uploads folder. You can create a folder with different name and Replace the Uploads folder with the name of your Folder.



Previous
Next Post »

3 comments

Click here for comments
Anonymous
admin
March 24, 2013 × This comment has been removed by a blog administrator.
avatar
December 24, 2013 ×

Very clearly explanation sir, but why you can't provide some sample of code each article you published?

Reply
avatar
December 24, 2013 ×

Thanks for your appreciations..i will start uploading source code for the articles very soon..so keep reading for for updates..:)

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..