How to rename image when upload and save into folder in serverhttp://forums.asp.net/t/1806001.aspx/1?How+to+rename+image+when+upload+and+save+into+folder+in+serverWed, 23 May 2012 15:50:00 -040018060014990523http://forums.asp.net/p/1806001/4990523.aspx/1?How+to+rename+image+when+upload+and+save+into+folder+in+serverHow to rename image when upload and save into folder in server <p><em><span style="text-decoration:underline">Split off from <a href="http://forums.asp.net/t/1589804.aspx/1?How&#43;to&#43;rename&#43;image&#43;when&#43;upload&#43;and&#43;save&#43;into&#43;folder&#43;in&#43;server"> http://forums.asp.net/t/1589804.aspx/1?How&#43;to&#43;rename&#43;image&#43;when&#43;upload&#43;and&#43;save&#43;into&#43;folder&#43;in&#43;server</a>&#43;</span></em></p> <p>Hey guys,</p> <p>I have the same problem please help me to solve it.</p> <p>I am new in programming and i need some help from you guys.</p> <p>I want remane my image file during uploading in local image folder.</p> <p>here is my code.</p> <pre class="prettyprint">protected void SubmitStoryObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e) { //upload image submitted by form into images folder FileUpload articleImageUpload = (FileUpload)SubmitStoryFormView.FindControl(&quot;txtStoryImage&quot;); if (articleImageUpload.HasFile) { articleImageUpload.SaveAs(Server.MapPath(&quot;C:\\Users\\WebDev2\\Desktop\\DH\\images\\&quot;) &#43; articleImageUpload.FileName &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;)); } e.InputParameters[&quot;StoryImage&quot;] = articleImageUpload.FileName; }</pre> <p>I'm not able to see image file on my image folder using this path.</p> <p>Please its very urgent.</p> <p>thanks in advance.</p> <p>Ashish</p> 2012-05-21T14:54:42-04:004990807http://forums.asp.net/p/1806001/4990807.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>1) Don't use Server.MapPath for already declared paths. It is used for handling stuff like &quot;~/MyFolder/Images/&quot;&nbsp; not C:\SomeFolder\</p> <p>2) It's not working because you are appending the datetime to the extension type. articleImageUpload.FileName contains the filename PLUS the extension.</p> <p>You want to do something like...</p> <pre class="prettyprint">articleImageUpload.SaveAs(@&quot;C:\Users\WebDev2\Desktop\DH\images\&quot; &#43; Path.GetFileNameWithoutExtension(articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(articleImageUpload.FileName));</pre> <p></p> 2012-05-21T18:42:12-04:004990821http://forums.asp.net/p/1806001/4990821.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Thanks Scott,</p> <p>It works fine now!!!!</p> <p>Ashish</p> 2012-05-21T18:50:42-04:004990985http://forums.asp.net/p/1806001/4990985.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p></p> <blockquote><span class="icon-blockquote"></span> <h4>N_EvilScott</h4> <p></p> <p>1) Don't use Server.MapPath for already declared paths. It is used for handling stuff like &quot;~/MyFolder/Images/&quot;&nbsp; not C:\SomeFolder\</p> <p>2) It's not working because you are appending the datetime to the extension type. articleImageUpload.FileName contains the filename PLUS the extension.</p> <p>You want to do something like...</p> <pre class="prettyprint">articleImageUpload.SaveAs(@&quot;C:\Users\WebDev2\Desktop\DH\images\&quot; &#43; Path.GetFileNameWithoutExtension(articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(articleImageUpload.FileName));</pre> <p></p> <p></p> </blockquote> <p></p> <p>Hi Scott,</p> <p>after changing the code. when i run on my production site. Data base was not picking up the picture from my Image folder.</p> <p>Can you please help me out to solve the issue?</p> <p>Here is the code.</p> <p>if (articleImageUpload.HasFile)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; articleImageUpload.SaveAs(Server.MapPath(&quot;&quot;) &#43; &quot;\\images\\&quot; &#43; Path.GetFileNameWithoutExtension(articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(articleImageUpload.FileName));&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p><strong>e.InputParameters[&quot;StoryImage&quot;] = articleImageUpload.FileName;</strong></p> <p>I think its not passing the image value to the paramerters.</p> <p>thanks,</p> <p>Ashish</p> <p></p> <p></p> 2012-05-21T22:40:50-04:004990988http://forums.asp.net/p/1806001/4990988.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Put a break point and see where its null. I bet you 5 bucks it's messing up with your Server.MapPath(&quot;&quot;) line.</p> <p><strong>EDIT</strong>: I think I skipped what you are saying... Is it saving the image ok, but not uploading it into your database?</p> 2012-05-21T22:45:03-04:004991018http://forums.asp.net/p/1806001/4991018.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p></p> <blockquote><span class="icon-blockquote"></span> <h4>N_EvilScott</h4> <p></p> <p>Put a break point and see where its null. I bet you 5 bucks it's messing up with your Server.MapPath(&quot;&quot;) line.</p> <p><strong>EDIT</strong>: I think I skipped what you are saying... Is it saving the image ok, but not uploading it into your database?</p> <p></p> </blockquote> <p></p> <p>Yes, Its not getting image from database to my articles.</p> <p></p> <p>if (articleImageUpload.HasFile)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; articleImageUpload.SaveAs(<wbr>Server.MapPath(&quot;&quot;) &#43; &quot;\\images\\&quot; &#43; Path.<wbr>GetFileNameWithoutExtension(<wbr>articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;<wbr>ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(<wbr>articleImageUpload.FileName));<wbr>&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p><strong>e.InputParameters[&quot;Image&quot;<wbr>] = articleImageUpload.FileName;</strong></p> <p></p> <p>e.InputParameters[&quot;Image&quot;] is passing value to database and when ever i call data base it gets the old value.</p> <p>Is there anyhitng which we can replace instant of</p> <p></p> <p><wbr><strong>Path.GetFileNameWithoutExtension(</strong><wbr><strong>articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;</strong><wbr><strong>ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(</strong><wbr><strong>articleImageUpload.FileName)</strong></p> <p>because i think it is not passing the same value.<strong></strong></p> <p>I'm new in ASP.NET so i dont know much about this stuff. Please help me.</p> <p>Thanks,</p> <p>AShish<strong><br> </strong></p> <p><strong><br> </strong></p> 2012-05-21T23:19:54-04:004991020http://forums.asp.net/p/1806001/4991020.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>I'm not exactly sure what you are trying to achieve.</p> <p>The code you posted will upload a file and save it to your images directory.</p> <p>Can you explain exactly what you are trying to do? for example...</p> <p>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...</p> <pre class="prettyprint">string savedFilePath = string.Empty; if (articleImageUpload.HasFile) { savedFilePath = Server.MapPath(&quot;&quot;) &#43; &quot;\\images\\&quot; &#43; Path.GetFileNameWithoutExtension(articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(articleImageUpload.FileName);</pre> <pre class="prettyprint"> articleImageUpload.SaveAs(savedFilePath); } e.InputParameters["Image"] = savedFilePath; </pre> <p></p> 2012-05-21T23:27:59-04:004991037http://forums.asp.net/p/1806001/4991037.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p></p> <blockquote><span class="icon-blockquote"></span> <h4>N_EvilScott</h4> <p></p> <p>I'm not exactly sure what you are trying to achieve.</p> <p>The code you posted will upload a file and save it to your images directory.</p> <p>Can you explain exactly what you are trying to do? for example...</p> <p>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...</p> <pre class="prettyprint">string savedFilePath = string.Empty; if (articleImageUpload.HasFile) { savedFilePath = Server.MapPath(&quot;&quot;) &#43; &quot;\\images\\&quot; &#43; Path.GetFileNameWithoutExtension(articleImageUpload.FileName) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(articleImageUpload.FileName);</pre> <pre class="prettyprint"> articleImageUpload.SaveAs(savedFilePath); } e.InputParameters["Image"] = savedFilePath;<br /></blockquote></pre> <p></p> <p>Let me check tomorrow and let you know if this one is working or not.</p> <p>Ashish</p> <p></p> <p></p> <p></p> <p></p> </blockquote> 2012-05-22T00:34:02-04:004992251http://forums.asp.net/p/1806001/4992251.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Scott,</p> <p>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?</p> <p>Also, here i have another same code but diffrence is it retuning diffrent value to data base.</p> <p>protected void Continue_Click(object sender, EventArgs e)</p> <p>&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl(&quot;adminHeader&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //upload image submitted by form into images folder<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileUpload articleImageUpload = (FileUpload)cph.FindControl(&quot;StoryImageTxtBox&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (articleImageUpload.HasFile)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; articleImageUpload.SaveAs(Server.MapPath(&quot;&quot;) &#43; &quot;\\../images\\&quot; &#43; Path.GetFileNameWithoutExtension(StoryImageTxtBox.FileName) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(StoryImageTxtBox.FileName));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewState[&quot;image&quot;] = StoryImageTxtBox.FileName;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>ViewState{&quot;image&quot;] link tothe data base for e.g:</p> <p><strong>ViewState[&quot;image&quot;] = articles.ElementAt(0).StoryImage.ToString();</strong></p> <p>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&nbsp; i.e.&nbsp; <strong>ViewState[&quot;image&quot;] = StoryImageTxtBox.FileName;</strong></p> <p>before i used your code i used to do </p> <p><strong>articleImageUpload.SaveAs(Server.MapPath(&quot;&quot;) &#43; &quot;\\../images\\&quot; &#43; StoryImageTxtBox.FileName);</strong></p> <p><strong>Please help me what i suppose to do.</strong></p> <p><strong>Ashish<br> </strong></p> <p></p> <p></p> 2012-05-22T13:43:49-04:004992639http://forums.asp.net/p/1806001/4992639.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Scott,</p> <p>Did you get chance to review my code?</p> <p>Please help if possible.</p> <p>thanks,</p> <p>ashish</p> <p></p> 2012-05-22T19:08:42-04:004992721http://forums.asp.net/p/1806001/4992721.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>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.</p> <p>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.</p> 2012-05-22T20:35:02-04:004992728http://forums.asp.net/p/1806001/4992728.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p></p> <blockquote><span class="icon-blockquote"></span> <h4>N_EvilScott</h4> <p></p> <p>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.</p> <p>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.</p> <p></p> </blockquote> <p></p> <p>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.</p> <p>protected void Continue_Click(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl(&quot;adminHeader&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //upload image submitted by form into images folder<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileUpload articleImageUpload = (FileUpload)cph.FindControl(&quot;StoryImageTxtBox&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string savedFilePath = string.Empty;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (articleImageUpload.HasFile)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; savedFilePath = Server.MapPath(&quot;&quot;) &#43; &quot;\\../images\\&quot; &#43; Path.GetFileNameWithoutExtension(<strong>StoryImageTxtBox.FileName</strong>) &#43; DateTime.Now.ToString(&quot;ddMMyyhhmmsstt&quot;) &#43; Path.GetExtension(<strong>StoryImageTxtBox.FileName</strong>);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; articleImageUpload.SaveAs(savedFilePath);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewState[&quot;image&quot;] = savedFilePath;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p></p> <p>In this code I can change the image file name but Im 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.</p> <p><strong>StoryImageTxtBox.FileName</strong> 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 <strong>StoryImageTxtBox.FileName</strong> to database which is link to <strong>ViewState[&quot;image&quot;]</strong></p> <p>Please give me some solution.</p> <p>Thanks,</p> <p>Ashish<br> <strong></strong></p> <p></p> <p></p> <p></p> 2012-05-22T20:50:01-04:004992732http://forums.asp.net/p/1806001/4992732.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Why not pass the updated filename?</p> 2012-05-22T20:57:50-04:004992735http://forums.asp.net/p/1806001/4992735.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p></p> <blockquote><span class="icon-blockquote"></span> <h4>N_EvilScott</h4> <p></p> <p>Why not pass the updated filename?</p> <p></p> </blockquote> <p></p> <p>how can i do that?</p> <p>here is my Old code.</p> <p>protected void Continue_Click(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl(&quot;adminHeader&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //upload image submitted by form into images folder<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileUpload articleImageUpload = (FileUpload)cph.FindControl(&quot;StoryImageTxtBox&quot;);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (articleImageUpload.HasFile)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; articleImageUpload.SaveAs(Server.MapPath(&quot;&quot;) &#43; &quot;\\../images\\&quot; &#43; StoryImageTxtBox.FileName);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewState[&quot;image&quot;] = StoryImageTxtBox.FileName;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp; code was like this before i changed to rename my file name.</p> <p></p> 2012-05-22T21:02:04-04:004994107http://forums.asp.net/p/1806001/4994107.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Hey Scott,</p> <p>Did u get chance to look at the problem?</p> <p>Please need your help if possible.</p> 2012-05-23T14:34:48-04:004994235http://forums.asp.net/p/1806001/4994235.aspx/1?Re+How+to+rename+image+when+upload+and+save+into+folder+in+serverRe: How to rename image when upload and save into folder in server <p>Hey Scott,</p> <p>I got the solution. thanks for your help.</p> <p></p> <p></p> <p></p> 2012-05-23T15:50:00-04:00