If you’ve been following this tutorial series you already know I’m a big fan of DMXzone´s Pure ASP Upload 3. It’s a great tool, but I wanted more. I soon learned that if I wanted to get the most out of Dreamweaver---and tools like Pure ASP Upload 3, I would have fine tune the code generated by Dreamweaver and PU3 to expand the possibilities. I quickly found that with a little hand coding, I could develop web applications as powerful, feature packed, and as versatile as anything I could code by hand. And I could do it in a lot less time.
Deleting the Image
We need two functions from Marcellino Bommezijn's tutorial on deleting images when updating or deleting a record. The first function, newFileSystemObject(), creates the file scripting object, and the second function fileExists uses the newly created file scripting object to determine if the file exists. I usually place the functions in an include file with other miscellaneous functions. You can also place them in the page, provided it's before you call them. Immediately above the MM_IIF Implementation is a good place.
In the last tutorial we delete only one image this time we're going to handle all three images using a for.. next loop and the sequential numerical names we gave our delete image checkbox our image fields and our image form fields. Before deleting the image(s) we use an if… then…. Statement to step through our images.
Tip: the for…next loop can used to through numerical values where the beginning and ending number are known or to loop through an array with an unknown number of members using the upper bound unbound and lower bound lbound properties.
For each field we check two conditions using an if… then statement: Is the user removing the existing is the user uploading a new image. If either or both are true then we delete the existing image. But before we delete the image we need to create our file scripting object to determine if the image exists on the server's file system.
Next we create a new file scripting object to actually delete the image, set the folder path, and the image file name. We'll get the file name from the hidden form field, upload_org_image_field, created by Pure ASP Upload which holds the original image value. We then call the fileExists function to make sure we have the file to delete. If we try to execute a delete where there's no file the page will throw an error. If the file exists we use the File.DeleteFile() method to remove the image from the server's file system. We close our If… Then statement and set the file back to nothing. Notice that we're using our counter i to step through the fields numerically. So that the names passed become Delete_Image1, Delete_Image2, Delete_Image3, and so with each field.
' check to see if the user has removed or replaced an image
and delete old i = 1 To 3
If UploadFormRequest("P_Image" & i & "") <> "" Or UploadFormRequest("Delete_Image" & i & "") <> "" Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
ImageFolder = Server.MapPath("..\site_images\")
ImagePath = ImageFolder & "\" & (UploadFormRequest("upload_org_P_Image" & i & ""))
' check if file exists and if true delete the file
If fileExists(ImagePath) Then File.DeleteFile(ImagePath)
Set File = Nothing
Finally the Dreamweaver generated update behaviour appends the query string to redirect to the desired page after the update.
' append the query string to the redirect URL
MM_editRedirectUrl = "default.asp"
If (UploadQueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & UploadQueryString
MM_editRedirectUrl = MM_editRedirectUrl & "&" & UploadQueryString