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.
Associated Images
If you're using Smart Image Processor ASP 2 like I am, then you may have some associated images to delete as well. In fact I create three versions of each image for my products and product versions in the DataSiteā¢ CMS V4.00: a thumbnail view, a regular view, and a large view. I use Advanced ToolTips and DMXzone Lightbox extensions to display the created images depending upon the situation inside my application. Deleting you additional images is really quite simple we just need to add capacity image call the fileExists function to make sure it's there in the line of code to delete the file. In code block below 'll notice I've added a second path, ThumbImagePath, and a third path, LargeImagePath.
For i = 1 To 3
If UploadFormRequest("P_LargeImage" & i & "")
<> "" Or UploadFormRequest("Delete_Image" & i & "") <> ""
Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
ImageFolder = Server.MapPath("..\product_images_large\")
ImagePath = ImageFolder & "\" &
(UploadFormRequest("upload_org_P_LargeImage" & i & ""))
ThumbImagePath = ImageFolder & "\thumb_" &
(UploadFormRequest("upload_org_P_LargeImage" & i & ""))
LargeImagePath = ImageFolder & "\large_" &
(UploadFormRequest("upload_org_P_LargeImage" & i & ""))
' check if file exists and if true delete the file
If fileExists(ImagePath) Then File.DeleteFile(ImagePath)
If fileExists(ThumbImagePath)
ThenFile.DeleteFile(ThumbImagePath)
If fileExists(LargeImagePath) Then File.DeleteFile(LargeImagePath)
Set File = Nothing
End If
Next
If you read the tutorial you should remember I used a multiline if… then statement is to determine if the fileExists and then delete it and added a closing End If. That was for clarity, in this more advanced tutorial I've used a single line if … then statement and there is no need for the closing End If. This method will keep your code a bit shorter.
Advanced Image Handling
In this portion of the tutorial, we'll look at an advanced case on page with images only with the fields sequentially numerically named. We will use a for… next loop to delete images and to step through the parameters. Sense you seen all this before you should have no trouble understanding the following code sample.
<%
If (CStr(UploadFormRequest("MM_update")) = "UpdateForm")
Then
If (Not MM_abortEdit) Then
' execute the update
Dim MM_editCmd
Set MM_editCmd = Server.CreateObject ("ADODB.Command")
MM_editCmd.ActiveConnection =
MM_datasource_STRING
MM_editCmd.CommandText = "UPDATE Products
SET P_LargeImage1 = ?, P_LargeImage2 = ?, P_LargeImage3 = ? WHERE P_ID = ?"
MM_editCmd.Prepared = true
' Loop through and toggle skip empty
field behavior to remove large images as needed
For image = 1 To 3
If CStr(UploadFormRequest("Delete_Image"
& image & "")) <> "" Then
MM_editCmd.Parameters.Append
MM_editCmd.CreateParameter("param" & image & "", 202, 1, 255,
UploadFormRequest("P_LargeImage" & image & "")) ' adVarWChar
Else
MM_editCmd.Parameters.Append
MM_editCmd.CreateParameter("param" & image & "", 202, 1, 255,
MM_IIF(UploadFormRequest("P_LargeImage" & image & ""),
UploadFormRequest("P_LargeImage" & image & ""), UploadFormRequest("upload_org_P_LargeImage"
& image & ""))) ' adVarWChar
End If 'End toggle
Next
MM_editCmd.Parameters.Append
MM_editCmd.CreateParameter("param4", 5, 1, -1, MM_IIF(UploadFormRequest("MM_recordId"),
UploadFormRequest("MM_recordId"), null)) ' adDouble
' check to see if the user has removed
or replaced an image and delete old images
For i = 1 To 3
If UploadFormRequest("P_LargeImage"
& i & "") <> "" Or UploadFormRequest("Delete_Image" & i &
"") <> "" Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
ImageFolder = Server.MapPath("..\product_images_large\")
ImagePath = ImageFolder & "\"
& (UploadFormRequest("upload_org_P_LargeImage" & i & ""))
ThumbImagePath = ImageFolder & "\thumb_"
& (UploadFormRequest("upload_org_P_LargeImage" & i & ""))
LargeImagePath = ImageFolder & "\large_"
& (UploadFormRequest("upload_org_P_LargeImage" & i & ""))
' check if file exists and if true
delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
If fileExists(ThumbImagePath) Then File.DeleteFile(ThumbImagePath)
If fileExists(LargeImagePath) Then File.DeleteFile(LargeImagePath)
Set File = Nothing
End If
Next
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
' append the query string to the redirect
URL
Dim MM_editRedirectUrl
MM_editRedirectUrl = "category.asp?Category_ID="
& (UploadFormRequest("Category_ID"))
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.