Click here to Skip to main content
15,886,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i dont want to overwrite file when the folder exists the same file name..please help me

What I have tried:

C#
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
    {
        string filename = Path.GetFileName(postedFile.FileName);
        postedFile.SaveAs(Server.MapPath("SiteLayout/" + filename));
        var extension = Path.GetExtension(filename);
        var nameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
        var i = 1;
        while (File.Exists("SiteLayout/" + filename))
        {
            filename = nameWithoutExtension.Trim() + " (" + i + ")" + extension;
            i++;
        }

        using (Stream fs = postedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
                {
                    string query = "insert into Site_Layout (ID,File_Path,Image_Path) values (@ID, @File_Path,@Image_Path)";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@File_Path", filename);
                        cmd.Parameters.AddWithValue("@Image_Path", "SiteLayout/" + filename);
                        cmd.Parameters.AddWithValue("@ID", TextBox1.Text);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        Label26.Visible = true;
                        Label26.Text = "File Uploaded Successfully";
                        con.Close();
                    }
                }
            }
        }
    }
    Label26.Visible = true;
   Label26.Text = "File Uploaded Successfully";

}
Posted
Updated 3-Jul-16 5:36am
v2

The problem is that when a user uploads a file, the name is quite likely to conflict with a different user's file of the same name - loads of people use the same set f file names every day!

The way I get round this is to store the user and the user file name in a DB, and store the file itself as a Guid filename: bb5a84b2-f9b6-47ae-98e1-25df6344788a.upload or similar. I then store the Guid in the DB so that I can cross-reference the file name the user wants to the "actual" file name in the file system.
When a user requests the file, i do it via an ASPX and a query string which contains the Guid value:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileTransferDownload.aspx.cs" Inherits="FileTransferDownload" %>

<%  
    // Send a download file to the client given the filename.    
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    Response.End();  
%>
 
Share this answer
 
this is not the solution to store the file name in db...
i just want to stop overwrite file which already exist in folder with the same name..
i use code but dont know why it is not working...
 
Share this answer
 
Comments
raju melveetilpurayil 4-Jul-16 12:36pm    
Put the Break points and debug your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900