Multiple Image Delete & Remove Options for Pure ASP Upload

This tutorial is the second of a three part series on image delete options using George Petrov's Pure ASP upload. The first shows how to handle image delete and remove options on an update form with a single image. This tutorial looks at update forms with multiple image fields. Having more than one image imposes limitations on image remove and delete options, but with a bit of imaginative thinking and a little hand coding we'll see that we can provide users with a full array of image handling options.

Look for UD code block below.

<%
' *** Update Record: (Modified for File Upload) set variables

If (CStr(UploadFormRequest("MM_update")) <> "" And CStr(UploadFormRequest("MM_recordId")) <> "") Then

MM_editConnection = MM_yoursite_STRING
MM_editTable = "News"
MM_editColumn = "N_ID"
MM_recordId = "" + UploadFormRequest("MM_recordId") + ""
MM_editRedirectUrl = "admin_news.asp"
MM_fieldsStr = "N_headline|value|N_image|value|N_paragraph|value"
MM_columnsStr = "N_headline|',none,''|N_image|',none,''|N_paragraph|',none,''"

' create the MM_fields and MM_columns arrays

Just below the UD comment in bold above is where we insert the code to toggle skip empty fields feature on or off. The skip empty fields feature default is on, so we're going to insert a bit of code that turns it off. This way the null value from the image form fields will update the database to a null value. All that's really needed is the first line beginning with if and the last line that closes the if then statement "end if." The other two lines are produced by PureASP UpLoad 2.07.

' toggle skip empty fields feature off
if UploadFormRequest("delete_image") = "" then
MM_columnsStr = FixColumnsForUpload(MM_fieldsStr,MM_columnsStr)
MM_fieldsStr = FixFieldsForUpload(MM_fieldsStr,MM_columnsStr)
end if


MM_fields = Split(MM_fieldsStr, "|")
MM_columns = Split(MM_columnsStr, "|")

' set the form values
For i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(i+1) = CStr(UploadFormRequest(MM_fields(i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And UploadQueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And UploadQueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & UploadQueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & UploadQueryString
End If
End If
End If
%>

In this section we check two conditions: The user is deleting the existing image or is uploading a new image. In either case, we delete the existing image. First we check if the database has a value for an existing image, then check if the image exists on the server, and if it exists, delete it. We use the two functions from Marcellino Bommezijn's great tutorial on deleting images when updating or deleting a record. Then we can follow his instructions and insert them here or they can used in include as well. (In the demo application you'll find the functions in functions.asp which is included in the update form page.)

<%
Function newFileSystemObject()
set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
%>
<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.fileExists(aFileSpec)
End Function
%>

Notice we place the functions above the UD comment in bold that begins the UD code block below.

<%
' *** Update Record: (Modified for File Upload) construct a sql update statement and execute it

If (CStr(UploadFormRequest("MM_update")) <> "" And CStr(UploadFormRequest("MM_recordId")) <> "") Then

' create the sql update statement
MM_editQuery = "update " & MM_editTable & " set "
For i = LBound(MM_fields) To UBound(MM_fields) Step 2
FormVal = MM_fields(i+1)
MM_typeArray = Split(MM_columns(i+1),",")
Delim = MM_typeArray(0)
If (Delim = "none") Then Delim = ""
AltVal = MM_typeArray(1)
If (AltVal = "none") Then AltVal = ""
EmptyVal = MM_typeArray(2)
If (EmptyVal = "none") Then EmptyVal = ""
If (FormVal = "") Then
FormVal = EmptyVal
Else
If (AltVal <> "") Then
FormVal = AltVal
ElseIf (Delim = "'") Then ' escape quotes
FormVal = "'" & Replace(FormVal,"'","''") & "'"
Else
FormVal = Delim + FormVal + Delim
End If
End If
If (i <> LBound(MM_fields)) Then
MM_editQuery = MM_editQuery & ","
End If
MM_editQuery = MM_editQuery & MM_columns(i) & " = " & FormVal
Next
MM_editQuery = MM_editQuery & " where " & MM_editColumn & " = " & MM_recordId

If (Not MM_abortEdit) Then
' execute the update
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection

Then we a insert version of the code from Marcellino Bommezijn's tutorial that has been modified with a for each statement to loop through the image fields. Be certain to note the last line of code above in bold black and the first line of code below in bold black. The creation of the fso and image delete must occur in this space—before the update query executes. We check to see if user selected the checkbox we created to remove all images. If either condition is true, we execute the delete.

' delete the file before we delete the record
' check to see if user deleted all images

if UploadFormRequest("delete_image") <> "" Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\site_images\")
for im = 1 to 3
ImageFile = ImagePath & "\" & (rs_about.Fields.Item("P_image"&im).Value)
' check if file exists and if true delete the file
If fileExists(ImageFile) Then
File.DeleteFile(ImageFile)
End If
next
end if

If you don't use numbers to differ your image fields, then you must do this. Remember what I said about considering your database design making things simpler?

' delete the file before we delete the record
'check to see if delete should run

if UploadFormRequest("delete_image") <> "" Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
' set path to images folder
ImagePath = Server.MapPath("..\site_images\")
ImageFile = ImagePath & "\" & (rs_about.Fields.Item("P_first_image").Value)
' check if file exists and if true delete the file
If fileExists(ImageFile) Then
File.DeleteFile(ImageFile)
End If
ImageFile = ImagePath & "\" & (rs_about.Fields.Item("P_second_image").Value)
' check if file exists and if true delete the file
If fileExists(ImageFile) Then
File.DeleteFile(ImageFile)
End If
ImageFile = ImagePath & "\" & (rs_about.Fields.Item("P_third_image").Value)
' check if file exists and if true delete the file
If fileExists(ImageFile) Then
File.DeleteFile(ImageFile)
End If
end if
end if

'end delete image code

Now we've handled the option to delete all the images and set all the database fields back to null values. But what if the user wants only to update on or two of the images? In that case, we don't have to worry about toggling the skip empty fields feature because we have a new image name for the database field. But we still want to get that old image off the server. So we're going to use little trickery to get our users to help us out with that on the next page.

Download Tutorial Files Nex Page > > >

James Threadgill

James ThreadgillJames Threadgill has authored numerous tutorials on ASP and ASP.NET web development, published on such sites as the Dynamic Zones and MSDN Accademic Alliance. He co-authored the Sam's book Dreamweaver MX: ASP.NET Web Development.

James first began computer programming in 1995 while attending Alvin Community College. He completed a certificate of computer science program and an Associate of Arts degree at Alvin before going on to the University of Houston-Clear Lake where he was awarded a Bachelor of Science and a Master of Arts.

James publishes fiction, poetry, and visual arts under the name Wayne James. His fiction first appeared in Raconteur in 1995 and since has been published numerous times: in Hadrosaur Tales 5 and 7, Bayousphere, and in the Write Gallery e-zine. His poetry first appeared in the small press magazine Lucidity in 1996 and has been published numerous times since. His collection of fiction and poetry, When Only the Moon Rages, was released in 2000. Most recently his work appeared in Tales of the Talisman winter 2010 and spring 2011 issues. James currently attends graduate school at the University of Houston and owns and operates small web design and internet marketing firm, WWWeb Concepts, with his wife, Karen, in Houston, TX USA.

See All Postings From James Threadgill >>

Comments

remove checkbox

October 8, 2003 by Marcello Citzia
Is there a possibility to insert a checkbox removeĀ for every single image?

How do I remove just 1 or 2 images?

September 11, 2004 by John Tucker

How can I use checkbox to "delete_images1" , "delete_images2" and "delete_images3" with out deleting all images?

You must me logged in to write a comment.