Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 4 February 2015

Introduction: I will explain how to validate file/image extension before uploading the file to the server using FileUpload control in asp.net using code in both C# and VB.Net languages.
Description: There may be requirement of uploading file of specific extension while developing website.  If you want the user can upload only .jpg, .jpeg, .png, .gif, .doc, .docx, .xls, .xlsx files  then you have to write the following code that can check the extension of the file the user is going to upload.


Implementation: Let's create an asp.net web application demonstrate the concept. 

  • Place a FileUpload control and a Button control on design page(.aspx) as:
  <asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="btnUpload" runat="server"  Text="upload"
            onclick="btnUpload_Click" />

C#.Net Code to validate file extension while uploading Document or Image
  • In the code behind (.aspx.cs) file write the code:
Include namespaces:


using System.IO;


then write the code on upload button's click event as:
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (IsValidExtension())
        {
            //write code to upload file
            string filePath = (Server.MapPath("Uploads/") + Guid.NewGuid() + FileUpload1.FileName);
            FileUpload1.SaveAs(filePath);
        }
        else
        {
            return;
        }
    }

    protected bool IsValidExtension()
    {
        if (FileUpload1.HasFile)
        {
            string FileExt = Path.GetExtension(FileUpload1.FileName);

FileExt = FileExt.ToLower();
            if (FileExt != "gif" && FileExt != ".jpg" && FileExt != "jpeg" && FileExt != "png" && FileExt != ".doc" && FileExt != ".docx" && FileExt != ".xls" && FileExt != ".xlsx")
            {
                Response.Write("Please upload gif, jpeg, jpg, png, doc, docx, xls, xlsx file only");
                return false;
            }          
        }
        return true;
    }

VB.Net Code to validate file extension while uploading Document or Image
  • In the code behind (.aspx.vb) file write the code:
First Include namespace:


imports System.IO;
then write the code on upload button's click event
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
                    If IsValidExtension() Then
                                         'write code to upload file
                                         Dim filePath As String = (Server.MapPath("Uploads/") + Guid.NewGuid() + FileUpload1.FileName)
                                         FileUpload1.SaveAs(filePath)
                    Else
                                         Return
                    End If
End Sub

Protected Function IsValidExtension() As Boolean
                    If FileUpload1.HasFile Then
                                         Dim FileExt As String = Path.GetExtension(FileUpload1.FileName)

FileExt = FileExt.ToLower()
                                         If FileExt <> "gif" AndAlso FileExt <> ".jpg" AndAlso FileExt <> "jpeg" AndAlso FileExt <> "png" AndAlso FileExt <> ".doc" AndAlso FileExt <> ".docx" AndAlso FileExt <> ".xls" AndAlso FileExt <> ".xlsx" Then
                                                             Response.Write("Please upload gif, jpeg, jpg, png, doc, docx, xls, xlsx file only")
                                                             Return False
                                         End If
                    End If
                    Return True
End Function


 Note: In this example Uploaded file will be saved in the Uploads folder.


Categories: , ,

0 comments:

Post a Comment