Uploading Images using HTML and ASP: Saving the File/Caching | WebReference

Uploading Images using HTML and ASP: Saving the File/Caching

Saving the File/Browser Caching for Masochists

Uploading Images using HTML and ASP

Saving the File

In which we insert the image into our Web site.

So now we have a canvas with an image on it. How do we get it into our Web site? You could upload it to a database and serve it when requested but here we're going to just save it out into an appropriate directory on our server.

Once again the code is very simple.

path = Server.MapPath("../images/final.jpg")

canvas.SaveAs path, "Quality=normal"

First we find the physical path to the hyperlink required using the standard MapPath call. Then we ask the canvas to save a copy of itself to this path. It will be saved as a JPEG because the file path specifies the file name as 'final.jpg'.

So why didn't we save it as 'final.gif'?

Unisys holds the patent on LZW compression which is used to produce GIF images. They have a policy that requires large fees to be paid for each Web server that uses this technology. To get around this problem, all Web toolkits produce uncompressed GIF images. The expansion in size depends on the type of image but may typically be a factor of between 2 and 3 for photo style images.

So although we could have saved it out as a GIF, JPEG is a better option unless you wish to use transparency.

There are techniques that allow you to produce 'real' LZW GIF images but these are complicated and require a fair investment of time.

Browser Caching for Masochists

In which we discuss the pain of browser image caching

Often it is quite enough to just save an image out to a directory and then reference it from other pages in your web site. However it is worth noting that browsers cache images in a different way from HTML. If you are going to be overwriting an image frequently you should consider the following.

If you replace an image which a browser has already cached it will not always reload it. Different browsers work in different ways but assuming the href to the image is the same some will pick up and reload the image immediately, some will reload it if you click on refresh, and some will only reload it if you do a forced refresh (command refresh). Most will pick up on the new image if you quit the browser and restart it but again this is dependent on browser settings. Herein lies our final Dark Secret Number 3.

- The only way to ensure that an image will be reloaded is to change the href

You can ensure that all your images are reloaded appropriately using a nifty piece of ASP. There are other more efficient ways of accomplishing this for different tasks but this does the job nicely for us.

We assign a directory for our image rather than a filename for our image. Each time we replace the image we change the filename. Each time a page of HTML references the image we use a piece of ASP code to look in the directory and insert the current filename. So what does the code for this look like?

To insert a file into the directory we use the following piece of code.

DeleteFilesFromURL("../images/final/")

Randomize()

path = Server.MapPath("../images/final/" & Int(1000000 * Rnd) & ".jpg")

canvas.SaveAs path, "Quality=normal"

Public Sub DeleteFilesFromURL (inFolderURL)

Set fso = Server.CreateObject("Scripting.FileSystemObject")

Set fold = fso.GetFolder(Server.MapPath(inFolderURL))

For Each file in fold.Files

ext = LCase(Right(file.Name, 4))

If ext = ".jpg" Then file.Delete

Next

End Sub

We start by deleting all the files from the directory. There should only be a maximum of one of these but you can't be too sure. After this we generate a random name for the file and save the image out with this name.

The DeleteFilesFromURL function is quite straightforward. It looks in the folder specified and iterates through all the files deleting any JPEGs. We limit it to JPEGs in case you accidentally drop your life's work - The Great American Novel - into the directory by mistake.

To insert the appropriate href into your page of HTML we use the following code.

GetFileFromURL("../images/final/")

Public Function GetFileFromURL (inFolderURL)

Set fso = Server.CreateObject("Scripting.FileSystemObject")

Set fold = fso.GetFolder(Server.MapPath(inFolderURL))

name = ""

For Each file in fold.Files

name = file.Name

Exit For

Next

GetFileFromURL = inFolderURL & name

End Function

All this does is to look in the directory specified and return the first file found. Couldn't be simpler, now all you've got to do is incorporate it into your site.


Comments are welcome

Created: Aug. 19, 1999 Revised: Aug. 19, 1999

URL: http://webreference.com/dev/upload/savecache.html