Compare uploaded file name without extension using C# and VB.Net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Hi

i have 2 fileuploads. i need to compare between two for same filename.

my problem is how to compare only the filename without extension of file.

Please help

        protected void Upload_Click(object sender, EventArgs e)
        {
            string shtml;
            string sxml;
            byte[] vcBytes = null, rawBytes = null;
            foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles)
            {
                string filename = Path.GetFileName(postedFile.FileName);
                string fileExtension = Path.GetExtension(postedFile.FileName);
                byte[] bytes = null;
                using (Stream fs = postedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bytes = br.ReadBytes((Int32)fs.Length);
                    }
                }
                if (fileExtension.ToLower() == ".htm")
                {
                    vcBytes = bytes;
                    shtml = System.IO.Path.GetFileNameWithoutExtension("vcBytes");
                }
                else
                {
                    Response.Write("<script>alert('Please select only Html file')</script>");
                }

            }
            foreach (HttpPostedFile postedFile in FileUpload3.PostedFiles)
            {
                string filename = Path.GetFileName(postedFile.FileName);
                string fileExtension = Path.GetExtension(postedFile.FileName);
                byte[] bytes = null;
                using (Stream fs = postedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bytes = br.ReadBytes((Int32)fs.Length);
                    }
                }
                if (fileExtension.ToLower() == ".xml")
                {
                    rawBytes = bytes;
                    sxml = System.IO.Path.GetFileNameWithoutExtension("rawBytes");

                }
                else
                {
                    Response.Write("<script>alert('Please select only XML file')</script>");
                }

            }
            if (FileUpload2.FileName==FileUpload3.FileName)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
                string query = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = conn;
                    cmd.Parameters.AddWithValue("@vcFilePath", vcBytes);
                    cmd.Parameters.AddWithValue("@Rawdata", rawBytes);
                    cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    Response.Write("<script>alert('File Upload Sucessfull')</script>");
                    RefreshPage();
                    DropDownList1.ClearSelection();
                    DropDownList2.ClearSelection();
                }
            }
            else
            {
                Response.Write("<script>alert('Please select same file')</script>");
                RefreshPage();
                DropDownList1.ClearSelection();
                DropDownList2.ClearSelection();
            }            
        }
Posted one month ago Modified on one month ago

Hi suhaas121,

Refer below code.

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;
using System.IO;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO 

Code

C#

protected void Upload_Click(object sender, EventArgs e)
{
    string shtml;
    string sxml;
    byte[] vcBytes = null, rawBytes = null;
    foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles)
    {
        string filename = Path.GetFileNameWithoutExtension(postedFile.FileName);
        string fileExtension = Path.GetExtension(postedFile.FileName);
        byte[] bytes = null;
        postedFile.SaveAs("~/File/" + filename);
        using (Stream fs = postedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                bytes = br.ReadBytes((Int32)fs.Length);
            }
        }
        if (fileExtension.ToLower() == ".htm")
        {
            vcBytes = bytes;
            shtml = System.IO.Path.GetFileNameWithoutExtension("vcBytes");
        }
        else
        {

            Response.Write("<script>alert('Please select only Html file')</script>");
        }
    }
    if (Path.GetFileNameWithoutExtension(FileUpload2.FileName) == Path.GetFileNameWithoutExtension(FileUpload3.FileName))
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
        string query = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@vcFilePath", vcBytes);
            cmd.Parameters.AddWithValue("@Rawdata", rawBytes);
            cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            Response.Write("<script>alert('File Upload Sucessfull')</script>");
            RefreshPage();
            DropDownList1.ClearSelection();
            DropDownList2.ClearSelection();
        }
    }
    else
    {
        Response.Write("<script>alert('Please select same file')</script>");
        RefreshPage();
        DropDownList1.ClearSelection();
        DropDownList2.ClearSelection();
    }
}

VB.Net

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("~/File/" & 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() = ".htm" Then
            vcBytes = bytes
            shtml = System.IO.Path.GetFileNameWithoutExtension("vcBytes")
        Else
            Response.Write("<script>alert('Please select only Html file')</script>")
        End If
    Next

    If Path.GetFileNameWithoutExtension(FileUpload2.FileName) = Path.GetFileNameWithoutExtension(FileUpload3.FileName) Then
        Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
        Dim query As String = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId"

        Using cmd As SqlCommand = New SqlCommand(query)
            cmd.Connection = conn
            cmd.Parameters.AddWithValue("@vcFilePath", vcBytes)
            cmd.Parameters.AddWithValue("@Rawdata", rawBytes)
            cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            Response.Write("<script>alert('File Upload Sucessfull')</script>")
            RefreshPage()
            DropDownList1.ClearSelection()
            DropDownList2.ClearSelection()
        End Using
    Else
        Response.Write("<script>alert('Please select same file')</script>")
        RefreshPage()
        DropDownList1.ClearSelection()
        DropDownList2.ClearSelection()
    End If
End Sub