Varun Sareen

Varun Sareen

  • NA
  • 24
  • 7.1k

Rename a file already uploaded

Dec 8 2011 2:44 AM
Dear Friends,
 
I have an Item Form Master where i am making an Item and here i have given a functionality of uploading an image for the item on a popup window and save it in a folder with the same name as image name.
 
Now when i am saving the item then i want to rename the name of the file uploaded but i am not able to rename the same.
 
The code with which i am trying to save the same is:-

if (!File.Exists(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName)))
{
string newImgName = Convert.ToString(itemID);
FileUpload file = new FileUpload();
FileInfo fileIn = new FileInfo(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName));
fileIn.Name.Replace(itemImgName + "." + fileExt, newImgName + "." + fileExt);
newImgName = newImgName + "." + fileExt;
new DAL.DatabaseActivities().ExecuteNonQuery("Update EBS_Client"+_compId+".dbo.ItemMaster set ItemImage='"+newImgName+"' where Item_Id="+itemID+"");
//file.SaveAs(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + file.FileName.Replace(itemImgName, newImgName)));
}
The above code is executing successfully but the image name doesn't get renamed as per my requirement.
 
Please let me know where i am going wrong or there is some other option of doing the same.

Answers (1)