Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file input dialog and upload onchange possible in IE?

This is basically and simplified what I have now:

<style>
form.noshow { height: 0; overflow: hidden; }
</style>

<form class=noshow target="SomeIframeThatExists">
  <input type=file id=uf>
</form>

<a id=uflink href="/user/photo">Upload photo</a>

<script>
$('uf').addEvent('change', function(e) {
  // alert('oele'); // this would work fine
  this.form.submit(); // auch in IE > "Access denied" exception
});
$('uflink').addEvent('click', function(e) {
  $('uf').click(); // opens file dialog in all browsers inc IE
  return false;
});
</script>

What it does (perfectly) in Chrome 11 and FF 4:

  1. The form is hidden.
  2. You click the link
  3. Choose file dialog opens
  4. You select a file
  5. Dialog closes
  6. Form is submitted
  7. Script in iframe is executed and photo is replaced

Very hightechlike and sweet.

In IE, all of that works, except [6]. The form isn't submitted. Javascript error: "Access denied". Doesn't matter how the form is invisible, as long as the dialog was opened with input.click() the form can't be submitted on change. (The onchange function is executed fine. The error only occurs when form.submit() is called.)

Now all of this I can accept. IE sucks. I live with it.

My solution so far was to check the navigator for "MSIE" and then when clicking the link instead of opening the dialog, showing the form (with the file input). Then the user has to click the actual, ugly file input and then everything works fine. But ugly.

The question is twofold:

  1. Is there a way to do this in IE as cool as it is in Chrome? WITHOUT nasty flash/java crap. Only html elements andjavascript.
  2. If not: is there a way to check for form.submit() support after opening the dialog from a link (besides !navigator.contains("MSIE"))?

[2] could be catching the "Access denied" exception thrown in IE, but then it's too late: the user has already opened the dialog and browsed to the photo. You don't wanna make him do that again. (Even IE users don't deserve that.)

PS. I'm only interested in Chrome 10+, Firefox 3.6+ and IE8+.

PS. Might be important: the file input element can't be anywhere near the link, because the link is inside a form and that form is (must be) separate from the file upload form.

UPDATE
Second best: detect support for this high-techlike behaviour that only doesn't work in IE. I don't wanna use navigator.appName.contains('MSIE') because that's not flexible and not necessarily true.

like image 307
Rudie Avatar asked May 01 '11 01:05

Rudie


People also ask

How do I open file upload dialog box on image click?

And on the button's click event write the jQuery code like : $('#OpenImgUpload'). click(function(){ $('#imgupload'). trigger('click'); });

How does input type file work?

Description. The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.

How do I upload a file to the click of icons?

Just add an label tag and wrap input tag inside label and hide input and give it a id which will be used on label for attribute. You can remove the for="..." if the input element is inside the label.

How do I open an input file?

If you cannot open your INPUT file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a INPUT file directly in the browser: Just drag the file onto this browser window and drop it.


2 Answers

@Rudie, up here - Thanks for that code! It works great in IE and Chrome, but not in FireFox.

I managed to take my old code (That works in FF and Chrome) and combined your code for MSIE.

Check it out here:

FIX FOR IE, CHROME AND FIREFOX

https://gist.github.com/4337047

PROBLEM: When an file-input is opened via a scripted, forced click() event, IE won't let you submit the form. If you click the file-input via your own mouse (what we don't want), IE will let you submit the form.

Please note that IE11, as of now, is allowing the form to submit if a file input field has changed via a scripted 'click' event.

Solution (partly thanks to Rudie @ Stackoverflow, https://stackoverflow.com/users/247372/rudie , http://hotblocks.nl/):

Make a label for the input in IE. If you click it, it will force a click on the input field - and IE will accept that (dumbass IE thinks user clicked the input field, hah)

So in that label we'll put our own styled DIV.

Next problem was, this doesn't work in FF. So we made a simple (possible nasty) browser check, and based on the browser we'll show a different button.

Solution is right here. Tested in:

  • Win 7 x64
  • FireFox 13.01
  • Chrome 23.0.1271.97 m
  • IE9 in regular IE9 mode

More tests / additions to code are MORE than welcome!

EDIT:

To quote Roy McKenzie

IE11 is now allowing the form to submit if a file input field has changed via a scripted 'click' event.

like image 147
Rob Avatar answered Sep 30 '22 04:09

Rob


I did it!!

http://jsfiddle.net/rudiedirkx/8hzjP/show/

<label for="picture">Upload picture</label>
<input type="file" id="picture" style="position: absolute; visibility: hidden" />

IE8 works. I don't care about others.

So easy =)

like image 42
Rudie Avatar answered Sep 30 '22 05:09

Rudie