/    Sign up×
Community /Pin to ProfileBookmark

Input type FILE…

Hi There.

I building a script in ASP that will allow the user to upload an image.

I want to make the following thing:
Whenever the user choose the imge he want to upload (by using the input typ=”file”) I want to show him the photo he choose in a height of 100px and relative width.

I also would like to check if the file he choose is valid: JPG or GIF.

Thanks for you help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@TageMay 29.2004 — Okay, the Gecko browsers weren't cooperating with the onChange event handler I put on the <input type="file"> element. So I came up with a bit longer of a workaround that makes it act like Internet Explorer and Opera. I used a userAgent check for the gecko browsers... By all means change this script to your liking. I only tested it with IE6.0, Netscape7.1, Mozilla FireFox, and Opera7.23...[code=php]<html>
<head>
<title>Image File Check-Preview</title>
<script type="text/javascript">
<!--
function checkFile(theValue,fromFocus){
if(typeof document.getElementById!="undefined"&&(fromFocus==null||(fromFocus==""&&navigator.userAgent.toLowerCase().indexOf("gecko")>-1&&lastImg!=theValue))){
lastImg=theValue;
if(theValue.slice(theValue.lastIndexOf(".")+1,theValue.length)!="gif"&&theValue.slice(theValue.lastIndexOf(".")+1,theValue.length)!="jpg"){
alert("This is not a valid file!nPlease choose a .gif or .jpg file!nThank You!")}
else{
document.getElementById("theImg").src="file:///"+theValue;}}}
//-->
</script>
</head>
<body>
<form name="theForm">
<input type="file" name="theFile" onChange="checkFile(this.value)" onClick="this.focus()" onFocus="checkFile(this.value,'')}">
<script type="text/javascript">
<!--
var lastImg=document.theForm.theFile.value;
//-->
</script>
<img src="blank.gif" alt="Preview of Image" height="100" id="theImg">
</form>
</body>
</html>[/code]
If they all worked with the onChange event handler, I would not have put in the onClick event handler, onFocus event handler, fromFocus var, or lastImg var. The onClick event handler focuses the file input element when the user clicks the field. If the user clicked the browse button...after they choose a file and click open, the focus() function is activated effectively firing off the onFocus event handler. The onFocus event handler has a second argument tagged onto checkFile(), two apostrophes. This is used to check if the event was fired from the onChange event handler or the onFocus event handler. I can then effectively seperate the gecko browsers from ie and opera. What the lastImg var does is keep a string of the last image's path that was loaded into the file input element. This is so I can check if the person clicked cancel by comparing lastImg to theValue. If they come up the same, there was no change in the field; therefore, the function checkFile() does not execute when the user clicks cancel. The line[code=php]var lastImg=document.theForm.theFile.value;[/code]sets the value of lastImg to the file input element's value when the page loads. I did this because sometimes when the user clicks reload, the value of the file input element isn't cleared. I set the height on the <img> tag to 100 so it would do what you wanted. Although, it might get really wide if someone has a horizontal picture. The line[code=php]typeof document.getElementById!="undefined"[/code]checks to see if the browser supports getElementById; if it does not, the script does not execute. Sorry for not having something that doesn't need a workaround; hopefully someone has a better way to do this, lol. Later...

Tage
Copy linkTweet thisAlerts:
@weeeauthorMay 30.2004 — Thanks a lot man!
×

Success!

Help @weee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...