Link to home
Start Free TrialLog in
Avatar of splanton
splantonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

jquery / Javascript - initiating file input to get around IE bug

I have a file button (for uploading an image).
I want to use an image to fire the 'file' selection. I have crated an image in a <label> tag and used 'for' to enable the <label> (and therefor the image) to fire the 'file' dialogue.

<label for = "userfile" style = "cursor: hand;cursor: pointer;">
	<input style = "display:none;" name="userfile" id="userfile" type="file" />
	<img src="<%=v_image%>">
</label>

Open in new window


The example shows my test code (apologies for the inline styling).

However there is a bug in IE that stops this working when you put the code inside a <form> tag. So firing manually using javascript/Jquery would seem to be a solution.

From my research there seems to be limitation with some browsers stopping the programmatic firing of a 'file' input, as this is deemed a potential security risk.

Does anyone have a solution that will allow me to get round this and allow the firing of a 'file' input when an image is clicked that will be cross browser compatible?

Regards
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Take a look at the many AJAX file uploaders.

They use an iframe to do exactly this.
Avatar of pierrejeanes
pierrejeanes

How about something like this:
<input type="file" id="userfile" name="userfile" style="visibility: hidden; width: 1px; height: 1px" >
<a href="" onclick="document.getElementById('userfile').click(); return false"><img src="your-image"</a>
ASKER CERTIFIED SOLUTION
Avatar of DualCool
DualCool

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial