[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 822
  • Last Modified:

Delete Image File with <asp:CommandField ShowDeleteButton-"True" />

I have a <asp:CommandField  ItemStyle-ForeColor="Red" ShowDeleteButton="True"  /> that runs a stored procedure against SQL 2005 and deletes a record containing the photos filepath in a table.  I would also like to DELETE the actual photp.jpg from the Images Folder on the Web Server.  How would I accomplish this?
0
DCRJDS
Asked:
DCRJDS
  • 2
1 Solution
 
gregg1ep00Commented:
System.IO.File.Delete( Server.MapPath( ResolveUrl( "~/images/photp.jpg" ) ) );

ResolveUrl method gives you the full path (in web url format) to your image (/webapp/images/photp.jpg)

Server.MapPath method changes that path to the physical location (C:\inetpub\wwwroot\...)
0
 
DCRJDSAuthor Commented:
on the editphoto.aspx page there is an <asp:GridView> that displays a table of photos that can be deleted from the Photo Table.(just the record).   When the user hits delete next to on of  the Photo sit runs the  (DeleteCommand="delete_photo" DeleteCommandType="StoredProcedure"  ) and it is taken out of the Photo table.  How do I use System.IO.File.Delete( Server.MapPath( ResolveUrl( "~/images/photp.jpg" ) ) );
from this same DELETECommand?
0
 
gregg1ep00Commented:
Sorry.  You'll have to handle the RowDeleting event and do it there:


      protected void gvw_RowDeleting( object sender, GridViewDeleteEventArgs e )
      {
            System.IO.File.Delete( Server.MapPath( ResolveUrl( (string) e.Values["PhotoFilename"] ) ) );
      }

Replace "PhotoFilename" with the name of the field in your database that identifies the location of the file.

Greg
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now