Solved

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

Posted on 2014-02-04
4
561 Views
Last Modified: 2014-02-19
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
0
Comment
Question by:splanton
4 Comments
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 39832069
Take a look at the many AJAX file uploaders.

They use an iframe to do exactly this.
0
 
LVL 2

Expert Comment

by:pierrejeanes
ID: 39832326
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>
0
 
LVL 58

Expert Comment

by:Gary
ID: 39832726
Something like this maybe
http://jsfiddle.net/GaryC123/tuJ27/3/

Fixed for FORM
http://jsfiddle.net/tuJ27/8/
0
 
LVL 2

Accepted Solution

by:
DualCool earned 500 total points
ID: 39860868
how about something like this-

>> css

#upload_wrap {position:absolute; top:25px; left:25px; width:332px; height:30px; cursor:pointer;}
#file_input {position:absolute; top:10px; left:10px; width:0px; height:0px; overflow:hidden; z-index:0;}
#browse_btn {position:absolute; top:0px; left:0px; width:60px; height:30px; background-color:#CCC; border:thin solid #333; text-align:center; z-index:1;}
#sel_name {position:absolute; top:0px; left:62px; width:300px; height:14px; border:thin solid #333; padding:8px; z-index:1;}

>>html


<div id="upload_wrap" onClick="document.getElementById('fi').click();">
    <div id="file_input">
          <input id="fi" type="file" onChange="document.getElementById('sel_name').innerHTML = this.value;"/><input />
    </div>
    <div id="browse_btn">Browse</div>    
    <div id="sel_name"></div>
</div>


***just dump it into your upload form
** could use some work - i spent about 5 mins on it -
FYI should be 100% cross browser
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question