Solved

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

Posted on 2014-02-04
4
566 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 55

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

839 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