Description of "showupload.asp" and "showimage.asp"

The form data is posted to "showupload.asp" which uses csASPUpload to extract the uploaded file as well as reading the file size and file name. The image data is then passed into the csImageFile component where it is resized.

Usually the file would then be saved to disk or input into a database but as this is a demonstration using somebody else's image, we have avoided doing this. The file data is stored in a session variable so it can be used later in the script "showimage.asp". This is a technique that should be treated with caution because storing large amounts of data in session variables can use a lot of memory. In this case the image is stored after resizing, and the session variable is reset immediately after use, so there should be no problems.

<%@ language=vbscript %>
Set Upload = Server.CreateObject("csASPUpload32.Process")
Set Image = Server.CreateObject("csImageFile.Manage")

If Request.TotalBytes < 600 * 1024 Then
  If Upload.FileQty > 0 Then
    If UCase(Upload.Extension(0)) = "JPG" or UCase(Upload.Extension(0)) _
    = "JPEG" Then
      On Error Resume Next
        Image.ReadStream "JPG", Upload.FileData(0)
      If Err <> 0 Then
        ImageError = true
        Success = true
      End If
      Image.JpegQuality = Upload.Value("JpegQuality")
      If Upload.Value("NewSize") = "w" Then
        Image.Resize 150, 0
      End If
      If Upload.Value("NewSize") = "h" Then
        Image.Resize 0, 150
      End If
      If Upload.Value("NewSize") = "s" Then
        Image.Scale 20
        End If

      Session("ImageData") = Image.JPGData

      NotAJPG = true
    End If

    NoFile = true
  End If
  NoFile = true
  FileTooBig = true
End If
<title> . . .

Most of the scripting is placed before the start of the HTML, and various Boolean variables are set if errors occur. These are used in the main body of the page to display conditional output. The errors checked for are the uploaded file being too big, the file not having the right extension or no file attached. There is also a check for failure to read a valid file into csImageFile. This could happen if the file is corrupt in some way.

The form variables must be read using the Value property of csASPUpload, because the Request.Form command does not work when the form is set for uploading.

The output used with a successful upload is shown below, with minimal formatting.

<p>The upload was successful and the image and some details are shown.</p>
<p>File name: <%=Upload.FileName(0)%><br>
Height: <%=Image.Height%><br>
Width: <%=Image.Width%><br>
Uploaded Size: <%=Upload.FileSize(0)%> KB<br>
Reduced Size: <%=Image.NewFileSize("JPG")%> KB</p>
<img src="showimage.asp">

The image is displayed using "showimage.asp". This script is shown below.

Response.Expires = 0
Response.Buffer = true

Response.ContentType = "image/jpeg"
Response.BinaryWrite Session("ImageData")
Set Session("ImageData") = nothing

The image is streamed using BinaryWrite and the data comes from the session variable stored by "showupload.asp". This session variable is set to nothing after use to reclaim the memory instead of waiting 20 minutes for it to clear itself.

Return to the upload form.


This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.