This tutorial explains how to write a code that deletes a file that has been uploaded to your server before deleting the record where the image belongs to.
June 18, 2001: Added a demo application.
Delete file before deleting record
This tutorial explains how to write a code that deletes a file that has been uploaded to your server before deleting the record where the image belongs to. Download an example application to see how to use this code (keep the directory structure when extracting this zip).
After you've created a delete page look for the following code and add the
blue and red lines to it.
What it does is that it first creates a variable for the delete command (blue
lines) and second creates a variable for the path to the filename (red lines).
I used in this example a directory 'images' where it can find the file in and
added the actual filename to it from the database.
<% If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then '
create the sql delete statement If
(Not MM_abortEdit) Then If
(MM_editRedirectUrl <> "") Then End If |
Note:
Keep in mind that you must open the recordset which holds the filename first!!!
In my example Iused a recordset named 'rsDelete' with a fieldname called 'filename'
in it.
After this we can go ahead and delete the file and record. That's all there
is to it.
Changelog:
June 18, 2001: Added a demo application.
I am working on an extension for this which will work with the 'PureASPUpload' from George Petrov.
Happy Coding!
Paul Keur (www.dreamsites.nl)
Comments
Nice
Thank You
Paul,
Thanks a lot. Great tip to complete Petrov's ASP FileUpload. Now uploading and managing files on the server have become easy tasks.
Keep up the good work!
Max
deleting multipe images
RE: deleting multipe images
Hello Leon,
Using the sample code:
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename").Value)
File.DeleteFile(ImagePath)
You could do a simple basic solution:
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename1").Value)
File.DeleteFile(ImagePath)
ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename2").Value)
File.DeleteFile(ImagePath)
Or you can use a 'FOR NEXT' loop where you add a variable to the filename which is a much nicer solution.
I am making an extension for this which will work with the 'PureAspUpload' from George Petrov. One of the features will be the option to select between single or multiple deletion.
Happy Coding!
Paul Keur
You must me logged in to write a comment.