Solved

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

Posted on 2014-02-04
4
571 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 56

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding a countdown to HTA 12 97
JQuery conflicts/syntax issue 4 24
Text color and background in a text box. 2 25
Ajax on ASP 2 36
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…

756 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