JAVASCRIPT previews image, checks image dimensions of width & height and checks image file size.

Problems: Won't work with Mozilla type browsers. Sometimes width and height aren't read. Very large files' sizes aren't read.

The way I came up with this script was that I needed a client-side script for my website customers that would check their image file sizes before uploads, because some folks don't know how to check file properties and were getting errors when server defaults prevented uploads larger than 200 kb.

I learned that it "isn't possible" to get image file sizes with javascript...however, I know that I HAVE seen scripts that do that, I just can't remember where... anyhow, I did find a script that would calculate the file size of a page: http://www.quirksmode.org/js/filesize.html. The thought occurred to me that if one had a script that would show a preview of an image upload that perhaps the previous script would include that image file in its calculations.

I found an image preview script that I liked on http://weblogs.macromedia.com/cantrell/archives/2004/06/how_to_implemen.cfm.

I don't know much about javascript code, so I experimented with getting the two scripts to work with each other until I came up with the following utility:


To implement in your ASP image upload form, perhaps you would want to open checkfile.htm from a popup window linked from your form. Open the Popup Window  Place the following between the HEAD tags of your page: (remove anything that is commented out that you don't need or uncomment it for use)
<script language="javascript">
//BEGIN FROM http://weblogs.macromedia.com/cantrell/archives/2004/06/how_to_implemen.cfm
var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i;
function previewImage(pathField, previewName)
{
var path = pathField.value;
if (path.search(imgRe) != -1)
{
document[previewName].src = 'file://'+path;
//END FROM http://weblogs.macromedia.com/cantrell/archives/2004/06/how_to_implemen.cfm

{
var img = new Image();
img.src = 'file://'+path;
document.write(img.width + ' pixels wide x ' + img.height +' pixels high<br>'+ '<img src=' + img.src + '><br>');

}

{
// BEGIN FROM: http://www.quirksmode.org/js/filesize.html
var size = (document.fileSize)*1;
var y = document.images;
var imglength = 0;
for (i=0;i<y.length;i++)
{
imglength += (y[i].fileSize)*1;
}
var total = size + imglength;
//uncomment the following to return more page info
//var writestring = 'File size HTML: ' + size;
//writestring += '\nFile size images: ' + imglength;
//writestring += '\nTotal file size: ' + total;
// END FROM: http://www.quirksmode.org/js/filesize.html
var writestring = 'Image file size: ' + imglength + ' bytes<br>' ;
if (imglength < 1) writestring += "<b>NOT APPLICABLE: \nFile too large for this script.</B><BR>";

//if (total > 40000) writestring += "\nFile too large!" + '<a href="javascript: history.go(-1)"><br>RETURN TO PREVIOUS PAGE</a>';

if (imglength > 40000) writestring += "\nFile too large!" + '<a href="javascript: history.go(-1)"><br>RETURN TO PREVIOUS PAGE</a>';

//if (total < 40000) writestring += "\nFile size OK." + '<a href="javascript: history.go(-1)"><br>RETURN TO PREVIOUS PAGE</a>';

//if (imglength >= 1)writestring += "\nFile size OK." + '<a href="javascript: history.go(-1)"><br>RETURN TO PREVIOUS PAGE</a>';

if (((40000 + imglength)<40000)) writestring +="\nFile size not OK." + '<a href="javascript: history.go(-1)"><br>RETURN TO PREVIOUS PAGE</a>' ;

if (((40000 + imglength)>40000)) writestring +="\nFile size OK." + '<a href="javascript: history.go(-1)"><br>RETURN TO PREVIOUS PAGE</a>' ;
document.write(writestring);

}

}
else
{
alert("JPG, PNG, and GIFs only!");
}
}
</script>

Place the following in the body of your page:

<form name="imageTest">
<input type="file"
name="myImage"
size="30" onChange="previewImage(document.imageTest.myImage,
'replaceMe')"/>
<br>
<img src="clear.gif" name="replaceMe" width="1" height="1"/>
</form>
ASP Examples Menu of Active Server Pages scripts code Classic ASP

Classic ASP Active Server Pages Examples, code, tutorials, scriptsASP Examples Menu (below*)



Active Server Pages | ASP | Scripts | Tutorials | Code | Web Programming | Examples
Active Server Pages ASP code examples, tutorials, and snippets for use in Programming interactive websites.

NOTE: Recently changed hosting and url so some demos aren't working now because I haven't updated pathing; if you find omissions or errors let me know.
 

*

Get Querystring key names along with querystring variables

ASP Youtube Downloader  Version FOUR
All ASP code. Allows user client side downloads of Youtube videos. You have the option to allow files to be saved directly to your site also.

ASP/PHP video downloader (Works better than ASP for large files)

Use ASP to dynamically parse a Youtube XML playlist to render to html.

Zip up an archive file of your site for backup if you need to restore website with ASP and free Zip Component.

Zip all files in a folder into an archive or backup file dynamically by specific file extension.

Sanitize or clean price cost field for database insertion or display.

Javascript image dimensions and image file size preview.

Put recordset into array, assign keys to the records then use record associated with specific array key as a variable elsewhere.

Access Database Interface Generator Wizard to read your table and field names and to create a table display.

Combining dropdown selection box menu with dynamic ASP database page.

How to redirect user to original page they tried to view before logging, after they are logged in.

Previous/Current/Next Records, or Buddy Ring Script

Custom ASP 404 page not found error script sends visitors to virtual folders

ASP Example: Combine ASP with Javascript for popup windows
dynamically sized to image dimensions.

ASP Example: How to use ASPJPEG to create thumbnails
Click for free Highly recommended thumbnailer for which you don't need Server access

ASP Example: What if your host has an older version of ASPJPEG that doesn't support gifs?

Using inline frames as "dynamic includes" (offline for updating)

Select Count Distinct Records for Access Database

Upload Image to folder and insert filename into database

Get date in such a way that it can be made part of a file name

Isolate a file name away from its path or extension or strip a path from a file name or strip characters from database field

User-customized on-the-fly stylesheet css files

Enhanced Page Hits Counter (also counts downloads and menu link hits)

Loop through array and compare to string with select case

Aphabetical Paging for your ASP scripts

Web Wiz Guide's Site Searcher Script modified as a file content indexer

Let Users Select How Many Records to View

Access Database for your PayPal IPN scripts

Miscellaneous collected code

You don't have to be a genius, just persistent, to write original Active Server Pages scripts

Software Recommendation: bare_bones_no_bells_and_whistles_asp_code_generator_database_interface_maker

--Lil at gmail.com

Some of my other website projects  
Horses and horsemen of Kansas Nebraska Iowa Missouri
Horses & Horsemen: Kansas, Nebraska, Iowa, Missouri
APHA Paint Horse videos
Paint Horse Videos
AQHA Quarter Horses videos
Quarter Horse Videos
Horse Training videos articles information free
Horse Training School
Western Pleasure Horse Training Manhattan Kansas