Get File Extension from File Upload control in the web form just by using Path.GetExtension Property in C#
Step 1: Create a web form in your project, In my case I named it as FileExtension.aspx and write the following design in it.
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
-
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="container">
- <br />
- <br />
-
-
- <div class="row">
- <div class="form-inline">
- <label > File Upload</label>
- <asp:FileUpload runat="server" ID="file_upload" CssClass="btn-default" />
-
- </div>
- <br />
-
- <div class="form-inline">
- <asp:Button ID="btnGet" runat="server" Text ="Get Extension" onclick="btnGet_Click" CssClass="btn-primary" />
- <asp:Label ID="lblResult" runat="server" style="color:green"/>
- </div>
- </div>
- </div>
- </form>
- </body>
- </html>
Step 2: Write the following C# code in
FileExtension.cs.
- protected void btnGet_Click(object sender, EventArgs e)
- {
- string strpath = System.IO.Path.GetExtension(file_upload.FileName);
- lblResult.Text = "Uploaded File Extension: " + strpath;
- }
Result in Browser:
Figure 1
Figure 2
Figure 3