locked
Delete an Image in Photo Album does not delete image from File System RRS feed

  • Question

  • User403614158 posted

    I need to know how to delete an image from the file system when the user deletes an image from a photo album.  Currently the sytem dletes the image location in the image table but leaves the image and it's children in the file sytem "~/pictures/".  How can I execute both on the button click?

    In the Photo.VB file I have tried this but it does not work:

      <System.ComponentModel.DataObjectMethodAttribute _
                    (System.ComponentModel.DataObjectMethodType.Delete, False)> _
            Public Shared Sub DeleteImage(ByVal id As Integer)
                Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
                If imageFileLocation = ImageUtils.ImageLocation.Database Then
                    Dim qry As New Query(Tables.Images)
                    qry.QueryType = QueryType.Delete
                    qry.AddWhere(Images.Columns.Id, id)
                    qry.Execute()
                Else
                    Dim qry As New Query(Tables.Image)
    
    
                    ' **********************************
    
                    Dim fileToDelete As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "~\Pictures\" & "large" & imageFileLocation)
                    fileToDelete.Delete()
    
    
                    ' **********************************
                    qry.QueryType = QueryType.Delete
                    qry.AddWhere(Image.Columns.Id, id)
                    qry.Execute()
    
                End If
            End Sub


    I know this has always been a problem with CSK, but ahs anyone figured it out yet?

    Thanks for any help!!

    KEN

    Wednesday, April 14, 2010 1:31 PM

All replies

  • User-863835478 posted

    We have to take the image's location string from the table to delete the image from the file system as soon as we delete the image location. If fileToDelete is the image file, try this:

     

    Dim fileToDelete As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "~\Pictures\" & "large" & imageFileLocation)   


            If (File.Exists(fileToDelete)) then
                File.Delete(fileToDelete)
            End if

     

    Please confirm back.

     

     

     

     

    Wednesday, April 14, 2010 6:04 PM
  • User403614158 posted

    Hi thuhue.  It did not work. I am getting an objectdatasource SelectImage no "id" error.

    Thursday, April 15, 2010 6:33 PM
  • User-863835478 posted

    Try this:

    Dim fileToDelete As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "~\Pictures\" & "large" & imageFileLocation)

    If fileToDelete IsNot Nothing then
       fileToDelete.Delete()
    End if
      

    Sunday, April 18, 2010 12:48 AM