So the issue is that you are storing the file on the images folder with a date time included, but you are only saving the filename to the database. You need to save the actual file name with the datetime so follow the example I posted above.
As far as the random date, when you call DateTime.Now it pulls the current Windows date and time from the web server running your website. If its pulling a wrong date that means your server has the wrong info on it.
So the issue is that you are storing the file on the images folder with a date time included, but you are only saving the filename to the database. You need to save the actual file name with the datetime so follow the example I posted above.
As far as the random date, when you call DateTime.Now it pulls the current Windows date and time from the web server running your website. If its pulling a wrong date that means your server has the wrong info on it.
I solve the issue with the image date and time. But i'm having problem in following code. this code is little bit diffrent then previous one.
In this code I can change the image file name but I’m having trouble to link to the data base: When it display the article, my database is not picking up the new renamed image. it is pointing to the old image file name if i see the source code.
StoryImageTxtBox.FileName contain the imformation for database. But because we rename the file with path.withextention and path.without extention i can not pass the image name from
StoryImageTxtBox.FileName to database which is link to ViewState["image"]
N_EvilScott
Star
8179 Points
1466 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 08:35 PM|LINK
So the issue is that you are storing the file on the images folder with a date time included, but you are only saving the filename to the database. You need to save the actual file name with the datetime so follow the example I posted above.
As far as the random date, when you call DateTime.Now it pulls the current Windows date and time from the web server running your website. If its pulling a wrong date that means your server has the wrong info on it.
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 08:50 PM|LINK
I solve the issue with the image date and time. But i'm having problem in following code. this code is little bit diffrent then previous one.
protected void Continue_Click(object sender, EventArgs e)
{
ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("adminHeader");
//upload image submitted by form into images folder
FileUpload articleImageUpload = (FileUpload)cph.FindControl("StoryImageTxtBox");
string savedFilePath = string.Empty;
if (articleImageUpload.HasFile)
{
savedFilePath = Server.MapPath("") + "\\../images\\" + Path.GetFileNameWithoutExtension(StoryImageTxtBox.FileName) + DateTime.Now.ToString("ddMMyyhhmmsstt") + Path.GetExtension(StoryImageTxtBox.FileName);
articleImageUpload.SaveAs(savedFilePath);
ViewState["image"] = savedFilePath;
}
In this code I can change the image file name but I’m having trouble to link to the data base: When it display the article, my database is not picking up the new renamed image. it is pointing to the old image file name if i see the source code.
StoryImageTxtBox.FileName contain the imformation for database. But because we rename the file with path.withextention and path.without extention i can not pass the image name from StoryImageTxtBox.FileName to database which is link to ViewState["image"]
Please give me some solution.
Thanks,
Ashish
N_EvilScott
Star
8179 Points
1466 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 08:57 PM|LINK
Why not pass the updated filename?
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 09:02 PM|LINK
how can i do that?
here is my Old code.
protected void Continue_Click(object sender, EventArgs e)
{
ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("adminHeader");
//upload image submitted by form into images folder
FileUpload articleImageUpload = (FileUpload)cph.FindControl("StoryImageTxtBox");
if (articleImageUpload.HasFile)
{
articleImageUpload.SaveAs(Server.MapPath("") + "\\../images\\" + StoryImageTxtBox.FileName);
ViewState["image"] = StoryImageTxtBox.FileName;
}
code was like this before i changed to rename my file name.
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 23, 2012 02:34 PM|LINK
Hey Scott,
Did u get chance to look at the problem?
Please need your help if possible.
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 23, 2012 03:50 PM|LINK
Hey Scott,
I got the solution. thanks for your help.