ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Compare uploaded file name without extension using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="suhaas121">suhaas121</a>,</p> <p>Refer below code.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Configuration; using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Configuration Imports System.IO </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Upload_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string shtml; &nbsp;&nbsp;&nbsp; string sxml; &nbsp;&nbsp;&nbsp; byte[] vcBytes = null, rawBytes = null; &nbsp;&nbsp;&nbsp; foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string filename = Path.GetFileNameWithoutExtension(postedFile.FileName); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string fileExtension = Path.GetExtension(postedFile.FileName); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] bytes = null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; postedFile.SaveAs(&#34;~/File/&#34; + filename); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (Stream fs = postedFile.InputStream) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (BinaryReader br = new BinaryReader(fs)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytes = br.ReadBytes((Int32)fs.Length); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (fileExtension.ToLower() == &#34;.htm&#34;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vcBytes = bytes; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shtml = System.IO.Path.GetFileNameWithoutExtension(&#34;vcBytes&#34;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(&#34;&lt;script&gt;alert(&#39;Please select only Html file&#39;)&lt;/script&gt;&#34;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (Path.GetFileNameWithoutExtension(FileUpload2.FileName) == Path.GetFileNameWithoutExtension(FileUpload3.FileName)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;connString&#34;].ConnectionString); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = &#34;update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlCommand cmd = new SqlCommand(query)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Connection = conn; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@vcFilePath&#34;, vcBytes); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Rawdata&#34;, rawBytes); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@iModuleId&#34;, TextBox2.Text); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn.Close(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(&#34;&lt;script&gt;alert(&#39;File Upload Sucessfull&#39;)&lt;/script&gt;&#34;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RefreshPage(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DropDownList1.ClearSelection(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DropDownList2.ClearSelection(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(&#34;&lt;script&gt;alert(&#39;Please select same file&#39;)&lt;/script&gt;&#34;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RefreshPage(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DropDownList1.ClearSelection(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DropDownList2.ClearSelection(); &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Upload_Click(ByVal sender As Object, ByVal e As EventArgs) Dim shtml As String Dim sxml As String Dim vcBytes As Byte() = Nothing, rawBytes As Byte() = Nothing For Each postedFile As HttpPostedFile In FileUpload2.PostedFiles Dim filename As String = Path.GetFileNameWithoutExtension(postedFile.FileName) Dim fileExtension As String = Path.GetExtension(postedFile.FileName) Dim bytes As Byte() = Nothing postedFile.SaveAs(&#34;~/File/&#34; &amp; filename) Using fs As Stream = postedFile.InputStream Using br As BinaryReader = New BinaryReader(fs) bytes = br.ReadBytes(CType(fs.Length, Int32)) End Using End Using If fileExtension.ToLower() = &#34;.htm&#34; Then vcBytes = bytes shtml = System.IO.Path.GetFileNameWithoutExtension(&#34;vcBytes&#34;) Else Response.Write(&#34;&lt;script&gt;alert(&#39;Please select only Html file&#39;)&lt;/script&gt;&#34;) End If Next If Path.GetFileNameWithoutExtension(FileUpload2.FileName) = Path.GetFileNameWithoutExtension(FileUpload3.FileName) Then Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;connString&#34;).ConnectionString) Dim query As String = &#34;update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId&#34; Using cmd As SqlCommand = New SqlCommand(query) cmd.Connection = conn cmd.Parameters.AddWithValue(&#34;@vcFilePath&#34;, vcBytes) cmd.Parameters.AddWithValue(&#34;@Rawdata&#34;, rawBytes) cmd.Parameters.AddWithValue(&#34;@iModuleId&#34;, TextBox2.Text) conn.Open() cmd.ExecuteNonQuery() conn.Close() Response.Write(&#34;&lt;script&gt;alert(&#39;File Upload Sucessfull&#39;)&lt;/script&gt;&#34;) RefreshPage() DropDownList1.ClearSelection() DropDownList2.ClearSelection() End Using Else Response.Write(&#34;&lt;script&gt;alert(&#39;Please select same file&#39;)&lt;/script&gt;&#34;) RefreshPage() DropDownList1.ClearSelection() DropDownList2.ClearSelection() End If End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/117671/Compare-uploaded-file-name-without-extension-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/117671/Compare-uploaded-file-name-without-extension-using-C-and-VBNet-in-ASPNet/Tue, 13 Aug 2019 03:51:05 GMT