Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file input

Status
Not open for further replies.

bartee

MIS
Mar 20, 2005
147
US
I want to allow users to upload pic files to my site but want to restrict the size -- say to 100*100.

Is there a relatively simple way to do this?

Thanks in advance.
 
your will need to use a server-side language such as asp or php(i prefer asp).
 

While some browsers can tell the dimensions of an image client-side (using JavaScript), as steven290 says, you are better off doing this server-side for maximum compatibility.

The actual upload bit has to be done server-side, regardless... so you may as well do the whole lot server-side.

Hope this helps,
Dan


The answers you get are only as good as the information you give!

 
Thanks for the feedback. I am using Cold Fusion and can actually upload the file with cffile.

However, I think I may still need the javascript to detect the size before uploading.

Any suggestions on that front.

Thanks again.
 
This works for me, client-side, to get the dimensions of an image:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		var tempImage;

		function showDimensions() {
			var imageName = document.forms[0].elements['myFile'].value;
			if (imageName != '') {
				imageName = 'file:///' + escape(imageName.split('\\').join('/'));
				imageName = imageName.split('%3A').join(':');

				tempImage = new Image();
				tempImage.onload = getDimensions;
				tempImage.src = imageName;
			}
		}

		function getDimensions() {
			alert(tempImage.width + ' x ' + tempImage.height);
		}
	//-->
	</script>
</head>

<body>
	<form>
		<input type="file" name="myFile" />
		<br />
		<input type="button" value="Show dimensions" onclick="showDimensions();" />
	</form>
</body>
</html>

It has been tested working in IE 6, FF 1, NN 7.1, and Opera 7.54. If you need to guarantee support in other browsers, you'd need to test it.

The string manipulation is a routine I copied from a post I wrote long ago:


The reason for it is:

1. I use .split(x).join(y) to do a quick search-and-replace of x with y... I hate writing regular expressions, especially ones that involve slashes...

2. I replace all \ with /, prepend file:///, escape the URL, and then assign to the source. IE spits the dummy if the : (as in C:) is escaped, so the second search-and-replace puts it back in. NN and Opera don't seem to mind about this.

Hope this helps,
Dan


The answers you get are only as good as the information you give!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top