1) Don't use Server.MapPath for already declared paths. It is used for handling stuff like "~/MyFolder/Images/" not C:\SomeFolder\
2) It's not working because you are appending the datetime to the extension type. articleImageUpload.FileName contains the filename PLUS the extension.
1) Don't use Server.MapPath for already declared paths. It is used for handling stuff like "~/MyFolder/Images/" not C:\SomeFolder\
2) It's not working because you are appending the datetime to the extension type. articleImageUpload.FileName contains the filename PLUS the extension.
I tried this morning and it does upload image on my folder with different name but instant of taking todays date it is showing some rendom date from 2008. so, do you have any idea why is it giving me diffrent date?
Also, here i have another same code but diffrence is it retuning diffrent value to data base.
I am having trouble during displying my article with image because database is not picking up new uploaded image. it just taking the old image file path for i.e.
ViewState["image"] = StoryImageTxtBox.FileName;
ashkorat
Member
7 Points
55 Posts
How to rename image when upload and save into folder in server
May 21, 2012 02:54 PM|LINK
Split off from http://forums.asp.net/t/1589804.aspx/1?How+to+rename+image+when+upload+and+save+into+folder+in+server+
Hey guys,
I have the same problem please help me to solve it.
I am new in programming and i need some help from you guys.
I want remane my image file during uploading in local image folder.
here is my code.
protected void SubmitStoryObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e) { //upload image submitted by form into images folder FileUpload articleImageUpload = (FileUpload)SubmitStoryFormView.FindControl("txtStoryImage"); if (articleImageUpload.HasFile) { articleImageUpload.SaveAs(Server.MapPath("C:\\Users\\WebDev2\\Desktop\\DH\\images\\") + articleImageUpload.FileName + DateTime.Now.ToString("ddMMyyhhmmsstt")); } e.InputParameters["StoryImage"] = articleImageUpload.FileName; }I'm not able to see image file on my image folder using this path.
Please its very urgent.
thanks in advance.
Ashish
N_EvilScott
Star
8179 Points
1466 Posts
Re: How to rename image when upload and save into folder in server
May 21, 2012 06:42 PM|LINK
1) Don't use Server.MapPath for already declared paths. It is used for handling stuff like "~/MyFolder/Images/" not C:\SomeFolder\
2) It's not working because you are appending the datetime to the extension type. articleImageUpload.FileName contains the filename PLUS the extension.
You want to do something like...
articleImageUpload.SaveAs(@"C:\Users\WebDev2\Desktop\DH\images\" + Path.GetFileNameWithoutExtension(articleImageUpload.FileName) + DateTime.Now.ToString("ddMMyyhhmmsstt") + Path.GetExtension(articleImageUpload.FileName));ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 21, 2012 06:50 PM|LINK
Thanks Scott,
It works fine now!!!!
Ashish
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 21, 2012 10:40 PM|LINK
Hi Scott,
after changing the code. when i run on my production site. Data base was not picking up the picture from my Image folder.
Can you please help me out to solve the issue?
Here is the code.
if (articleImageUpload.HasFile)
{
articleImageUpload.SaveAs(Server.MapPath("") + "\\images\\" + Path.GetFileNameWithoutExtension(articleImageUpload.FileName) + DateTime.Now.ToString("ddMMyyhhmmsstt") + Path.GetExtension(articleImageUpload.FileName));
}
e.InputParameters["StoryImage"] = articleImageUpload.FileName;
I think its not passing the image value to the paramerters.
thanks,
Ashish
N_EvilScott
Star
8179 Points
1466 Posts
Re: How to rename image when upload and save into folder in server
May 21, 2012 10:45 PM|LINK
Put a break point and see where its null. I bet you 5 bucks it's messing up with your Server.MapPath("") line.
EDIT: I think I skipped what you are saying... Is it saving the image ok, but not uploading it into your database?
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 21, 2012 11:19 PM|LINK
Yes, Its not getting image from database to my articles.
if (articleImageUpload.HasFile)
Server.MapPath("") + "\\images\\" + Path.GetFileNameWithoutExtension(articleImageUpload.FileName) + DateTime.Now.ToString("ddMMyyhhmmsstt") + Path.GetExtension(articleImageUpload.FileName));
{
articleImageUpload.SaveAs(
}
e.InputParameters["Image"] = articleImageUpload.FileName;
e.InputParameters["Image"] is passing value to database and when ever i call data base it gets the old value.
Is there anyhitng which we can replace instant of
because i think it is not passing the same value.
I'm new in ASP.NET so i dont know much about this stuff. Please help me.
Thanks,
AShish
N_EvilScott
Star
8179 Points
1466 Posts
Re: How to rename image when upload and save into folder in server
May 21, 2012 11:27 PM|LINK
I'm not exactly sure what you are trying to achieve.
The code you posted will upload a file and save it to your images directory.
Can you explain exactly what you are trying to do? for example...
Upload and save it to my folder, then save the file path in the database so I can display it again later? If thats the case then do this...
string savedFilePath = string.Empty; if (articleImageUpload.HasFile) { savedFilePath = Server.MapPath("") + "\\images\\" + Path.GetFileNameWithoutExtension(articleImageUpload.FileName) + DateTime.Now.ToString("ddMMyyhhmmsstt") + Path.GetExtension(articleImageUpload.FileName);ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 12:34 AM|LINK
Let me check tomorrow and let you know if this one is working or not.
Ashish
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 01:43 PM|LINK
Scott,
I tried this morning and it does upload image on my folder with different name but instant of taking todays date it is showing some rendom date from 2008. so, do you have any idea why is it giving me diffrent date?
Also, here i have another same code but diffrence is it retuning diffrent value to data base.
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\\" + Path.GetFileNameWithoutExtension(StoryImageTxtBox.FileName) + DateTime.Now.ToString("ddMMyyhhmmsstt") + Path.GetExtension(StoryImageTxtBox.FileName));
ViewState["image"] = StoryImageTxtBox.FileName;
}
ViewState{"image"] link tothe data base for e.g:
ViewState["image"] = articles.ElementAt(0).StoryImage.ToString();
I am having trouble during displying my article with image because database is not picking up new uploaded image. it just taking the old image file path for i.e. ViewState["image"] = StoryImageTxtBox.FileName;
before i used your code i used to do
articleImageUpload.SaveAs(Server.MapPath("") + "\\../images\\" + StoryImageTxtBox.FileName);
Please help me what i suppose to do.
Ashish
ashkorat
Member
7 Points
55 Posts
Re: How to rename image when upload and save into folder in server
May 22, 2012 07:08 PM|LINK
Scott,
Did you get chance to review my code?
Please help if possible.
thanks,
ashish