I am not sure how to allow my users to delete their "profile" image. The image is not stored in a database, local a folder. The existing code for the page is below. I believe I will need to add a button or use "href" but I am not sure what code would
be used. Any help is much appreciated.
Thanks for the response. I did add the code, it appears to exucute through the Response.Redirect("~/Profile")
but the image does disappear. I did add ")" to the DeleteImage code (below) but added everything else as list.
Any thoughts on what I could change. I seems clear how you are calling the image but I am not sure why it isn't working. I am re-copying the two sets of code below.
That deleted the image from the folder, thank you! It would appear that the cache is not clearing or something else is happening.
After I execute the delete method, the image is no longer in the folder but the profile image continues to appear on the web page. If I log out and log back in, the image still appears. If I clear the history, cookies, etc. in IE, the is gone but the red
"x" image appears and it does not display the default image.
When I use "Response.Redirect("~/Profile");", does the web page load and run fresh each time or is there a way to force it to?
You need to delete the image name from the database too. Assuming that each user only has one image at a time in the Files table, you can just add a line or two to the DeleteImage file:
var image = UrlData[0];
if(!image.IsEmpty() && image.IndexOf("/") < 1){
if(File.Exists(Path.Combine(Server.MapPath("~/images/ProfileImages"),image))){
File.Delete(Path.Combine(Server.MapPath("~/images/ProfileImages"),image));
var db = Database.Open("Mumba_Data");
db.Execute("DELETE FROM Files WHERE UserId = @0", WebSecurity.CurrentUserId);
}
}
Response.Redirect("~/Profile");
Would you have any idea why this code would not work when I transfer the site from a local server to a live server on Godaddy? When I click on the Delete Image link, I receive the 404 (Page Not Found) Error. I have not changed the code and it works fine
on my local server.
After looking at this further, I believe the conflict is here...
The line of code "<a href="~/DeleteImage/@image.Filename">" is being read as "http://www.mumba.me/DeleteImage/3_matt.jpg" and that page does not exist. On the local server, the "@image.Filename" indicated which image is to be deleted using the code on that
page. I retested it on the local server and it works as is.
1. Use the code as <a href="@Href(~/DeleteImage", image.Filename)">
Also the Paths is correct.
But you can try to write the code for deletion. And the name should bt outsider (I mean deleteImage can be the page and 3_matt.jpg can be the file to be worked)
There in that page use
var delete_Image = UrlData[0];
From this you can get the name. And after that you can delete the image by code you are using.
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
kookoomoo
Member
54 Points
92 Posts
Deleting Image From Folder
Nov 10, 2012 12:08 PM|LINK
Hello,
I am not sure how to allow my users to delete their "profile" image. The image is not stored in a database, local a folder. The existing code for the page is below. I believe I will need to add a button or use "href" but I am not sure what code would be used. Any help is much appreciated.
Thanks.
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Profile"; } @{ int UserId = WebSecurity.CurrentUserId; var db = Database.Open("Mumba_Data"); var SQLSELECT = "SELECT * FROM ProfileInfo WHERE UserId=@0"; var mydata = db.Query(SQLSELECT, UserId); var grid = new WebGrid(source: mydata); var sql = "SELECT * FROM Files WHERE UserId = @0"; var image = db.QuerySingle(sql, WebSecurity.CurrentUserId); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Profile</title> <style type="text/css"> .grid { margin: 4px; border-collapse: collapse; width: 850px; } .grid th, .grid td { border: 1px solid #C0C0C0; padding: 5px; } .head { background-color: #E8E8E8; font-weight: bold; color: #FFF; } .alt { background-color: #E8E8E8; color: #000; } </style> </head> <body> <h1>Profile</h1> <div id="ProfileInfo"> @grid.GetHtml( tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt", columns: grid.Columns( grid.Column(format: @<a href="EditProfile?ProfileId=@item.ProfileId">Edit</a>), grid.Column("FirstName"), grid.Column("LastName"), grid.Column("CompanyName"), grid.Column("Address1"), grid.Column("Address2"), grid.Column("City"), grid.Column("State"), grid.Column("Zip"), grid.Column("OfficePhone"), grid.Column("MobilePhone") ) ) </div> <div id="ProfileImage"> @if(image == null){ <img src="~/images/ProfileImages/superman.jpg" alt="" /> } else { <img src="~/images/ProfileImages/@image.Filename" alt="" /> } <p><a href="AddProfileImage">Add Profile Picture</a></p> </div> </body> </html>Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Deleting Image From Folder
Nov 10, 2012 04:02 PM|LINK
If you want to delete a file from a folder, you can use the File.Delete() method. You can add a lin to a file called DeleteImage.cshtml:
<div id="ProfileImage"> @if(image == null){ <img src="~/images/ProfileImages/superman.jpg" alt="" /> } else { <img src="~/images/ProfileImages/@image.Filename" alt="" /> <p><a href="~/DeleteImage/@image.Filename">Delete Image</a></p> } <p><a href="AddProfileImage">Add Profile Picture</a></p> </div>In the DeleteImage fiile, you check to see if the file exists, then delete it and redirect:
var image = UrlData[0]; if(!image.IsEmpty() && image.IndexOf("/") < 1){ if(File.Exists(Server.MapPath("~/images/ProfileImages" + image){ File.Delete(Server.MapPath("~/images/ProfileImages" + image); } } Response.Redirect("~/Profile");Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
kookoomoo
Member
54 Points
92 Posts
Re: Deleting Image From Folder
Nov 10, 2012 06:53 PM|LINK
Hi Mike,
Thanks for the response. I did add the code, it appears to exucute through the Response.Redirect("~/Profile") but the image does disappear. I did add ")" to the DeleteImage code (below) but added everything else as list.
Any thoughts on what I could change. I seems clear how you are calling the image but I am not sure why it isn't working. I am re-copying the two sets of code below.
Thanks!
Page: Pofile.cshtml
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Profile"; } @{ int UserId = WebSecurity.CurrentUserId; var db = Database.Open("Mumba_Data"); var SQLSELECT = "SELECT * FROM ProfileInfo WHERE UserId=@0"; var mydata = db.Query(SQLSELECT, UserId); var grid = new WebGrid(source: mydata); var sql = "SELECT * FROM Files WHERE UserId = @0"; var image = db.QuerySingle(sql, WebSecurity.CurrentUserId); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Profile</title> <style type="text/css"> .grid { margin: 4px; border-collapse: collapse; width: 850px; } .grid th, .grid td { border: 1px solid #C0C0C0; padding: 5px; } .head { background-color: #E8E8E8; font-weight: bold; color: #FFF; } .alt { background-color: #E8E8E8; color: #000; } </style> </head> <body> <h1>Profile</h1> <div id="ProfileInfo"> @grid.GetHtml( tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt", columns: grid.Columns( grid.Column(format: @<a href="EditProfile?ProfileId=@item.ProfileId">Edit</a>), grid.Column("FirstName"), grid.Column("LastName"), grid.Column("CompanyName"), grid.Column("Address1"), grid.Column("Address2"), grid.Column("City"), grid.Column("State"), grid.Column("Zip"), grid.Column("OfficePhone"), grid.Column("MobilePhone") ) ) </div> <div id="ProfileImage"> @if(image == null){ <img src="~/images/ProfileImages/superman.jpg" alt="" /> } else { <img src="~/images/ProfileImages/@image.Filename" alt="" /> <p><a href="~/DeleteImage/@image.Filename">Delete Image</a></p> } <p><a href="~/AddProfileImage">Add Profile Picture</a></p> </div> </body> </html>Page: DeleteImage.cshtml
@{ var image = UrlData[0]; if(!image.IsEmpty() && image.IndexOf("/") < 1){ if(File.Exists(Server.MapPath("~/images/ProfileImages" + image))){ File.Delete(Server.MapPath("~/images/ProfileImages" + image)); } } Response.Redirect("~/Profile"); }Thanks Again!
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Deleting Image From Folder
Nov 11, 2012 06:23 AM|LINK
That wasn't the only mistake in my code. I have concatenated the image name with the folder name. The file will never exist. Change is as follows:
if(File.Exists(Path.Combine(Server.MapPath("~/images/ProfileImages"), image))){ File.Delete(Path.Combine(Server.MapPath("~/images/ProfileImages"), image)); }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
kookoomoo
Member
54 Points
92 Posts
Re: Deleting Image From Folder
Nov 11, 2012 02:04 PM|LINK
That deleted the image from the folder, thank you! It would appear that the cache is not clearing or something else is happening.
After I execute the delete method, the image is no longer in the folder but the profile image continues to appear on the web page. If I log out and log back in, the image still appears. If I clear the history, cookies, etc. in IE, the is gone but the red "x" image appears and it does not display the default image.
When I use "Response.Redirect("~/Profile");", does the web page load and run fresh each time or is there a way to force it to?
Thanks,
Robbie
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Deleting Image From Folder
Nov 11, 2012 03:34 PM|LINK
You need to delete the image name from the database too. Assuming that each user only has one image at a time in the Files table, you can just add a line or two to the DeleteImage file:
var image = UrlData[0]; if(!image.IsEmpty() && image.IndexOf("/") < 1){ if(File.Exists(Path.Combine(Server.MapPath("~/images/ProfileImages"),image))){ File.Delete(Path.Combine(Server.MapPath("~/images/ProfileImages"),image)); var db = Database.Open("Mumba_Data"); db.Execute("DELETE FROM Files WHERE UserId = @0", WebSecurity.CurrentUserId); } } Response.Redirect("~/Profile");Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
kookoomoo
Member
54 Points
92 Posts
Re: Deleting Image From Folder
Nov 12, 2012 02:18 AM|LINK
Thank you.
kookoomoo
Member
54 Points
92 Posts
Re: Deleting Image From Folder
Dec 30, 2012 11:13 AM|LINK
Hi Mike,
Would you have any idea why this code would not work when I transfer the site from a local server to a live server on Godaddy? When I click on the Delete Image link, I receive the 404 (Page Not Found) Error. I have not changed the code and it works fine on my local server.
Any help you can provide is much appreciated.
Thanks!
Robbie
kookoomoo
Member
54 Points
92 Posts
Re: Deleting Image From Folder
Dec 30, 2012 12:56 PM|LINK
After looking at this further, I believe the conflict is here...
The line of code "<a href="~/DeleteImage/@image.Filename">" is being read as "http://www.mumba.me/DeleteImage/3_matt.jpg" and that page does not exist. On the local server, the "@image.Filename" indicated which image is to be deleted using the code on that page. I retested it on the local server and it works as is.
Not sure how to proceed from here.
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Deleting Image From Folder
Dec 30, 2012 07:49 PM|LINK
You should have started a new thread!
1. Use the code as <a href="@Href(~/DeleteImage", image.Filename)">
Also the Paths is correct.
But you can try to write the code for deletion. And the name should bt outsider (I mean deleteImage can be the page and 3_matt.jpg can be the file to be worked)
There in that page use
From this you can get the name. And after that you can delete the image by code you are using.
~~! FIREWALL !~~