This is a downloadable demo showing how to use our ASPPhotoResizer and ASPFileSaver components to save uploaded JPEG images using ASP and resizing them at the same time. It forms a miniature application allowing images to be uploaded, viewed and deleted. Both the ASPPhotoResizer and ASPFileSaver components must be registered on the server before this demo can be run. These components and the demo files can be downloaded using the links below.
Download the demo - imageresize.zip (4 KB)
Download the ASPPhotoResizer trial (340 KB)
Download the ASPFileSaver trial (131 KB)
The demo, imageresize.zip, contains the following files.
| File | Description |
|---|---|
| fileupload.htm | The starting page. This contains an HTML form for uploading a file to the server. |
| filesave.asp | The script that resizes and saves the image. This script calls the ASPFileSaver component to capture the file and ASPPhotoResizer to resize and save it. |
| view.asp | This shows the files that have been uploaded. It uses the File System Object to do this. |
| delete.asp | This uses the File System Object to delete a previously uploaded file. |
| readme.txt | A description of the application, including some troubleshooting tips. |
Some of the important features are explained below.
This page contains a form with the input type=file tag allowing the user to upload a file. The "enctype" attribute must be set as shown:
<form method="post" action="filesave.asp" enctype="multipart/form-data">
<input type="file" name="filesent">
<input type="submit" value="Send File">
</form>
This script uses ASPFileSaver to capture the uploaded file and ASPPhotoResizer to resize it. First, an instance of each component is created:
Set Upload = Server.CreateObject("ASPFileSaverTrial.FileSave")
Set JPG = Server.CreateObject("ASPPhotoResizerTrial.Resize")
The rest of the script consists of two nested If..Then..Else statements. The first checks if a file has been uploaded. The second checks that the extension of the file is .jpg or .jpeg. If both tests are true the image can be read into ASPPhotoResizer to resize and save it. Appropriate messages are written for each condition. One further test has not been included, and that is to use On Error Resume Next around the VariantIn command to allow graceful failure if the image is unreadable.
If Upload.FileSize > 0 Then
If (LCase(Upload.FileExtension) = "jpg") or _
(LCase(Upload.FileExtension) = "jpeg") Then
MaxWidth = 150
MaxHeight = 100
JPG.VariantIn Upload.FileAsVariant
JPG.ResizeBox MaxWidth, MaxHeight
JPG.SaveToFile Server.MapPath("./files/") & "\" & Upload.FileName
'File saved
Else
'Incorrect file type
End If
Else
'No file uploaded
End If
The ResizeBox function will reduce the size of the image to make it fit into an area specified by the width and height parameters. These are the variables MaxWidth and MaxHeight. The ResizeImage function would require some additional logic to do this. The SaveToFile command requires a full physical path and in this case it is constructed using Server.MapPath.
These scripts do not use our component so we will leave the reader to view the code to see how they work. They use the File System Object to list files in a folder and to delete selected files.
© Chestysoft, 2008.