16

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.

10
  • I have a feeling it might be doable for IE using some sort of activex or vbscript hackery, but not exactly an expert at those so can't really help with that. Commented May 1, 2011 at 3:49
  • I suspect the problem is with the fact you're sending this into iframe - try posting to the same page and see if it works. Commented May 1, 2011 at 10:37
  • @Jani I don't want to use ActiveX or VB hackery =) Is there no better way than checking navigator.appVersion for "MSIE"?? Commented May 1, 2011 at 13:07
  • @Shadow The error occurs when submitting the form, not because it's submitting into an iframe. (I tried it.) Same error in the same place and no page reload or anything =( If you can't submit the form, why the hell does IE allow the file choose dialog to open?? Commented May 1, 2011 at 13:10
  • @Rudie worked for me with IE8... can you post link to live example? Commented May 1, 2011 at 13:40

5 Answers 5

12

@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.

Sign up to request clarification or add additional context in comments.

5 Comments

Browser sniffing is unacceptable for me. +1 for the successful effort though.
Thanks, but why is that unacceptable for you? I know its not really that nice, but still - does it really matter that much? Its a workaround for Internet Explorer, and just and only Internet Explorer.
Yes it matters, because browser sniffing doesn't mean anything. You can spoof user agent string. Maybe IE will update for it to work. It might work in IE 9. Etc. Feature detection would be acceptable, but that seems impossible.
Okay, but why would you spoof your browsers' name? Go on the web with IE pretending to be Chrome and then whine about sites that don't work.. yeah :P Also, the detection function will most likely be updated as it is jQuery. Just keep jQuery up to date and you're good to go.
IE11 is now allowing the form to submit if a file input field has changed via a scripted 'click' event. Wahooo! I just tested the interaction in IE11 (which no longer has MSIE in it's userAgent string beeteedubs).
8

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 =)

4 Comments

Thanks for this ! It works fine on Chrome and IE8 but not on Firefox though. I noticed that the trick has nothing to do with the "setTimeout" as one might think, it appears that the label goes on top of the hidden input and that's enough to "fake" the click.
Huh... It doesn't work in Firefox. Damnit! So it's not perfect. Or even acceptable. checking all my old code
Another version, but this one doesn't do so well in IE (9). jsfiddle.net/rudiedirkx/8hzjP/68/show Probably because not all of the file input opens the dialog in IE. If only IE just played nice for a change......
Finally! Just add <label> pointing to input element and it works!!!! So easy!!! Silly IE!!
2

Strange indeed, IE8 block the submission only if there's enctype="multipart/form-data" in the form.

One workaround that worked for me locally is adding "real" submit button e.g.

<input type="submit" id="btnSubmit" value="Submit" />

Then have such code to submit:

$('btnSubmit').click();

If this works you can probably hide the button with CSS to make it transparent to the user.

11 Comments

Nope =( No dice. (But that would have been so cool.) js2.hotblocks.nl Did I do something wrong?
@Rudie when I just change the file it works fine - only when clicking the link it denies access - same behavior for you?
@Shadow Yup, same here. For some reason IE(8 and 9) allows the file dialog through the link, but doesn't allow the form submit when it's been used. I'm baffled! 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.)
@Rudie let me mess with this some more when I'll have time.. keep reminding me in case I forget!
By all means, do =) As will I. As said before: the "Upload photo" link can't be (anywhere near) the file input, because the link is in another (simpler, not file upload) form.
|
2

Well, this is the EXACT same problem I'm having right now. And only (disgusting) hack that did solve is to make the input[type=file] quite big with CSS, make it alpha=0 and put it on top of your intended UI element.

In short, you're making the user click the ugly "browse" button without him/her realizing.

Comments

0

Try adding the tag enctype="multipart/form-data" to your form element.

1 Comment

As you can see on js2.hotblocks.nl, I already have: <form target="photoframe" method="post" action="" enctype="multipart/form-data">. The upload actually works in FF and Chrome, so the correct enctype is necessary and present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.