Jump to content

Display image preview - file input incorrect...


richrock

Recommended Posts

Hi, 

 

Having a little difficulty with some JS to preview a local image:

 

	<script type="text/javascript">
	<!-- Begin
		/* This script and many more are available free online at
		The JavaScript Source!! http://javascript.internet.com
		Created by: Abraham Joffe :: http://www.abrahamjoffe.com.au/ */

		/***** CUSTOMIZE THESE VARIABLES *****/

		  // width to resize large images to
		var maxWidth=180;
		  // height to resize large images to
		var maxHeight=200;
		  // valid file types
		var fileTypes=["bmp","gif","png","jpg","jpeg"];
		  // the id of the preview image tag
		var outImage="previewField";
		  // what to display when the image is not valid
		var defaultPic="";

		/***** DO NOT EDIT BELOW *****/

		function preview(what){
		  var source=what.value;
		  alert(source);
		  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
		  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
		  globalPic=new Image();
		  if (i<fileTypes.length) globalPic.src=source;
		  else {
			globalPic.src=defaultPic;
			alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
		  }
		  setTimeout("applyChanges()",200);
		}
		var globalPic;
		function applyChanges(){
		  var field=document.getElementById(outImage);
		  var x=parseInt(globalPic.width);
		  var y=parseInt(globalPic.height);
		  if (x>maxWidth) {
			y*=maxWidth/x;
			x=maxWidth;
		  }
		  if (y>maxHeight) {
			x*=maxHeight/y;
			y=maxHeight;
		  }
		  field.style.display=(x<1 || y<1)?"none":"";
		  field.src=globalPic.src;
		  field.width=x;
		  field.height=y;
		}
	// End -->
</script>

 

And this is run with an input type="file" :

 

<input type="file" id="picField" onchange="preview(this)">

 

Which *should* display here:

 

<img alt="Graphic will preview here" id="previewField" src="">

 

Problem is, the JS fires, but the input tag strips the file location from :

 

C:/Documents and Settings/<user>/My Pictures/image.jpg

 

to

 

image.jpg

 

Which results in a broken image link because there is no image.jpg...  Any ideas how/why?  I've tried 3 different scripts and it seems that they all do the same.  :wtf:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.