Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday, 6 February 2015

Introduction
 It is always better to validate the file extension before uploading it i.e. if you want the user can upload only .jpg, .jpeg, .png, .gif , .bmp files then you have to check the extension of the file the user is going to upload before uploading.
Suppose, somewhere in your application if you want to upload profile pic, in that case you have to check the extension of the picture before uploading. So that nobody can upload file with different file extension.
Here is the example with code:


  • 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 the code behind (.aspx.cs) file write the code: In this example we create a function to validate file extension. If extension of the files is same as we required then this function will return "true". Then we call this function at "btnUploadImage_Click" event of button. If function will return true value then file will be uploaded to the server, otherwise it will give a message "Please upload .jpeg,.png,.gif,.jpg,.bmp image only".
    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 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 the above application I have uploaded the images to a folder i.e. “Uploads”.

0 comments:

Post a Comment