Pure ASP Upload 3 Review by James Threadgill
In this tutorial we will look at how to take your image handling options to the limit
DMXzone’s Pure ASP Upload is one of the best Tools I have for creating dynamic web sites. I’ve been using it over ten years. But there’s never really enough? Is there? Dreamweaver is a great tool for creating dynamic website, but if you want to get the most out of Dreamweaver and tools like DMXzone Pure ASP Upload, you have to reach under the hood and get your hands dirty by fine-tuning your code. With a little hand coding, however, there is no reason your applications cannot be as powerful and feature packed as any available.
Deleting the Image
Before we can delete the image we check two conditions: Is the user removing the existing image or is the user uploading a new image. In either case, 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.
First we get the two functions from Marcellino Bommezijn's great 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. However 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
%>
Now let's take a look at the delete code. We use the UploadFormRequest collection to ask for two values: the delete image checkbox value and image upload field value. If either has a value other than null then the image delete code executes.
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.
' Delete the file before we update the record
If CStr(UploadFormRequest("Delete_Image")) <> "" Or CStr(UploadFormRequest("Link_Image")
<> "") Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
ImageFolder = Server.MapPath("..\site_images\")
ImagePath = Imagefolder & "\" & (UploadFormRequest("upload_org_Link_Image"))
'if file exists delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
Set File = Nothing
End If 'end delete file
Finally the Dreamweaver generated update behavior 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 = "add_links.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
%>
Thumb Image
Now that we've deleted the image associated with the record, we may also need to delete a thumbnail image--if you're using DMXzone Smart Image Processor to create thumbnails of your uploaded images. Let's quickly look at how to modify our code block to delete the thumbnail image as well. To do this we must set the path to the thumb image well.
In the modified code block below you'll notice I've added a second path, ThumbImagePath. Then I call the fileExists function to make sure the thumb image exists. Notice I've modified the file spec passed for the thumb image. I've also modified the file spec passed in the File.DeleteFile method for the thumb image. Now close the If… Then statement and that's all there is to it.
' Delete the file before we update the record
If CStr(UploadFormRequest("Delete_Image")) <> "" Or CStr(UploadFormRequest("Link_Image")
<> "") Then
'create file scripting object
Set File = CreateObject("Scripting.FileSystemObject")
ImageFolder = Server.MapPath("..\site_images\")
ImagePath = ImageFolder & "\" & (UploadFormRequest("upload_org_Link_Image"))
ThumbImagePath = ImageFolder & "\thumb_" &
(UploadFormRequest("upload_org_Link_Image"))
'if file exists delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
If fileExists(ThumbImagePath) Then
File.DeleteFile(ThumbImagePath)
End If
Set File = Nothing
End If 'end delete file
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.