Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Advanced
Feb 5, 2003

Allow a Preview of an Image File Selected for Upload


This script gives a preview of an image file selected for upload (even before the upload process takes place). It works in both IE and Netscape. This way, users can see which image is being uploaded, thereby decreasing the chances of wrong files being uploaded.
 
<input type="file" id="attachfile" onFocus="dolayer();"
onclick="dolayer();" onkeydown="return false;">
<BR><BR>
<div id="imgdiv" style="border: medium solid steelblue; 
height: 300px; width: 300px; text-align: center; 
vertical-align: middle; font-family:
Verdana; font-size: 10pt; color: navy; 
font-weight: bold;">
	<BR><BR><BR><BR><BR>
<BR><BR><BR>No file uploaded.
</div>

<script language="JavaScript">
function dolayer()
{
	if ( document.getElementById('attachfile').value == '' )
	{
		var img = 
document.getElementById('imgdiv');
		img.innerHTML = 
'<BR><BR><BR><BR><BR><BR>
<BR><BR>No file uploaded.';
		}
		else
		{
			var filename = 
document.getElementById('attachfile').value;
			var fileextension = 
filename.substring(filename.length - 3);
			//alert(fileextension);
			if ( fileextension == 
'JPG' || fileextension == 'jpg' || fileextension
== 'GIF' || fileextension == 'gif' || fileextension == 'BMP' ||
fileextension == 'bmp')
			{
				var layerText = 
'<img name=image1
src="file:\/\/\/'+document.getElementById('attachfile').value +'"
width=300 height=300>';
		var img = document.getElementById('imgdiv');
		img.innerHTML = layerText;
				//alert(layerText);
			}
			else
			{
		var img = document.getElementById('imgdiv');
		img.innerHTML = "<BR><BR><BR>
<BR><BR><BR>A " + fileextension + " file
has been attached. <BR> 
Preview available only for Image files" ;
			}
		}
	}
</script>
Sanjay Nair
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date