Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends
I want to upload a file into my projects Folder(..\TextFiles\Blogs).
But I want to upload files by unique name so that niether two files with same name
can be uploaded nor override the existing file.
My Code is as below:
protected void blgBtn_Click(object sender, EventArgs e)
       {

           string fileName = blgFile.FileName;
           string filePath = blgFile.PostedFile.FileName;
           int fileLength = blgFile.PostedFile.ContentLength;
           blgFile.SaveAs(Server.MapPath("~/TextFiles/Blogs") + fileName);
           byte[] imageBytes = new byte[fileLength];
           blgFile.PostedFile.InputStream.Read(imageBytes, 0, fileLength);

           if (System.IO.File.Exists(filePath))
           {
               int counter = 2;
               while (System.IO.File.Exists(filePath))
               {
                   // if a file with this name already exists,
                   // prefix the filename with a number.
                   string tempfileName = counter.ToString() + fileName;
                   filePath = filePath + tempfileName;
                   counter++;
               }


               cmd = new SqlCommand("AddImg", con);
               cmd.CommandType = CommandType.StoredProcedure;
               SqlParameter[] prms = new SqlParameter[5];
               //cmd.Parameters.AddWithValue("BlogName",blgTb.Text);
               cmd.Parameters.AddWithValue("BlogPath", blgFile.ToString());
               //cmd.Parameters.AddWithValue("BlogFile",imageBytes);
               cmd.Parameters.AddWithValue("Sequence", int.Parse(Seq.Text));
               cmd.Parameters.AddWithValue("Page", int.Parse(blgPage.Text));
               cmd.Parameters.AddWithValue("Date", DateTime.Parse(dtpCalendar.Text));
               cmd.Parameters.AddWithValue("Comment", blgComment.Text);
               con.Open();
               cmd.ExecuteNonQuery();
               con.Close();


If it not fine then suggest me write code.

Thanks in Advance

With Best Regards
Akhilesh Pathak
Posted
Comments
Orcun Iyigun 1-Mar-13 2:36am    
I think the way do it is fine. Another suggestion will be you can use the DateTime.Now and format it while saving the file.
Orcun Iyigun 1-Mar-13 3:17am    
I think you SHOULD check if file exists before saving it. Because from your code what I understand is you save it and right after you check if the file exists :)

1 solution

u have use File.Exists method
C#
using System.IO;
string FileChk = Server.MapPath("~/TextFiles/Blogs") + fileName;
if (File.Exists(FileChk ))
{
   //then file is exist

}
 
Share this answer
 
Comments
Orcun Iyigun 1-Mar-13 2:37am    
Have you even looked at his code? He is already doing it..
Pallavi Waikar 1-Mar-13 2:40am    
but he is not using Server.MapPath ..he is only checking file name...but not in folder in which he want to store file...if i am wrong..please tell me reason....i am also trying to learn here
Orcun Iyigun 1-Mar-13 3:16am    
OK I understand. he coded it while saving the file but during not checking it.

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