Be the first to write a review
Pure ASP Upload 3 Review by James Threadgill, Part 2
In this tutorial we will look at how to use multiple image handling
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.
<%
Function newFileSystemObject()
set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.fileExists(aFileSpec)
End Function
%>
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
End If
Next
Finally the Dreamweaver generated update behaviour appends the query string to redirect to the desired page after the update.
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
' append the query string to the redirect URL
Dim MM_editRedirectUrl
MM_editRedirectUrl = "default.asp"
If (UploadQueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0)
Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" &
UploadQueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&"
& UploadQueryString
End If
End If
Response.Redirect(MM_editRedirectUrl)
End If
End If
%>
James Threadgill
James 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.