This tutorial explains how to delete a file before the update or delete sql statement is executed.
PureUpload delete file before delete/update
This tutorial will explain how to delete a file while using an update form or a delete form in conjunction with PureASPUpload 1 & 2. The article of Paul Keur also explains this, but i had some questions from people which couldn't get the picture with all the code in the demo pages. It's actually very simple. First of all you will need to these two functions to make it work. <%
Find your code for either the delete or update sql statement that starts with: Delete statement: <% Update Statement: Just above these sql statements place the two functions as mentioned above. Then insert the code as shown below in the update or delete statement: ' execute the delete/update As you see it also will work for an update ! This is very handy when using it for example a news administration with images, where it is allowed to edit/change images. This way all images will be deleted from your system/server. One important note is that the database field that is called from the recordset, in this example (rsNews.Fields.Item("image").Value), needs to be opened before you call this database field. So, if your recordset is below the sql statement for update or delete, move it to the very top. Example code of the recordset: <% The below example would delete multiple files, uploaded with PureASPUpload. Creating a dynamic array and using the statement For Each..Next to get access to this array. Setting up the array with the fields of the database that holds the filename of the file uploaded: <% ImagePath1 = Server.MapPath("upload\") ImagePath2 = Server.MapPath("upload\") ImagePath3 = Server.MapPath("upload\") ImagePath4 = Server.MapPath("upload\") Dim ImageLibrary(3) Using the For Each..Next statement to access the array and set the delete action: If (Not MM_abortEdit) Then Hope this answers the requests for some people, for deleting multiple files. Note: the ending slash has to be in to make it work in the Server.MapPath.
For some reason it works with me without this slash (?)
|
Marcellino Bommezijn
Marcellino Bommezijn is one of the managers at dmxzone.com. He is a contributor on the tutorials section.
Owner of Senzes Media (http://www.activecontent.nl) which provides professional services and web applications for mid-sized companies.
ActiveContent CMS is the ASP.NET Content Management solution that is used for building professional and rich-featured websites.
Comments
Needing help
Is this where the location of the file being deleted is supposed to be? ImagePath = Server.MapPath("..\upload")
I followed your tutorial exactly. I didn't add anything to the recordset. Do I need to? All I did with the recordset was move it to the top.
After following the tutorial this is the error I am getting.
Error Type:
ADODB.Fields (0x800A0CC1)
Item cannot be found in the collection corresponding to the requested name or ordinal.
/wilkinsons/admin/adminhouseland/delete.asp, line 94
Line 94 is this code ImagePath = ImagePath & "\" & (deletehomes.Fields.Item("image").Value) deletehomes is the name of the recordset.
Any ideas?
Thanks in advance,
Adam Toohey
RE: Needing help
Any ideas?
I have been trying to get this working for a long time now and would really appreciate some help getting this working.
Looking forward to a response.
Adam Toohey
RE: Needing help
Adam,
This is the code that you actually add in the update/delete statement:
' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\upload")
ImagePath = ImagePath & "\" & (rsNews.Fields.Item("image").Value)
' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
Of course you have to rename your own recordset and corresponding database fields. The database field "image" is just an example. But you already solved that one.
Now you only have to determine what the relative path is to your directory where the image/file resides. The path ("..\upload") is also an example of what it could be. So depending on your location and name of that directory in your root in could be different. For example ("..\..\images") or ("..\files").
Marcellino
RE: RE: Needing help
Hi Marcellino,
I have done all of this and still the file isn't being deleted. I am sure there is something easy that I haven't done. This is the code I have included in my page.....
<%
Function newFileSystemObject()
set newfilesystemobject="Server".CreateObject("Scripting.FileSystemObject")
End Function
%>
<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.FileExists(aFileSpec)
End Function
%>
<%
' *** Delete Record: construct a sql delete statement and execute it
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
' create the sql delete statement
MM_editQuery = "delete from " & MM_editTable & " where " & MM_editColumn & " = " & MM_recordId
If (Not MM_abortEdit) Then
' execute the delete
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\..\images\jpgs\houseland")
ImagePath = ImagePath & "\" & (deletehomes.Fields.Item("thumbnailpic").Value)
' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)
End If
End If
Usually i would put a / after putting the destination in example ../../images/jpgs/houseland/ How come there is no / after the destination in your code. I will also send you the entire page. Maybe you can find time to have a look.
Thanks,
Adam
You must me logged in to write a comment.