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.

Rudie
  • 52,220
  • 42
  • 131
  • 173
  • 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. – Jani Hartikainen May 01 '11 at 03: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. – Shadow The GPT Wizard May 01 '11 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"?? – Rudie May 01 '11 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?? – Rudie May 01 '11 at 13:10
  • @Rudie worked for me with IE8... can you post link to live example? – Shadow The GPT Wizard May 01 '11 at 13:40
  • @Shadow I can't, because that's behind a few layers of auth and I can't just give passwords away. I will try to make an exact-as-possible copy you can try. – Rudie May 01 '11 at 13:47
  • Live example: http://hotblocks.nl/tests/ajax/brupload.php – Rudie May 01 '11 at 13:57
  • @Shadow I have very much simplified the live example. It still doesn't work in my IE8. And it still does in my Chrome and FF. – Rudie May 01 '11 at 16:51
  • ` – Rudie Sep 14 '11 at 14:05
  • +1: Was having the same problem, but assumed it was the `.submit()` (on a multipart form) that was causing the security exception. Having the user click a temporarily visible `input:file` was a reasonable workaround for me, so, thanks! – Dmitri Aug 15 '12 at 03:41

5 Answers5

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.

Community
  • 1
  • 1
Rob
  • 4,927
  • 4
  • 26
  • 41
  • 1
    Browser sniffing is unacceptable for me. +1 for the successful effort though. – Rudie Dec 19 '12 at 21:58
  • 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. – Rob Dec 20 '12 at 12:12
  • 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. – Rudie Dec 20 '12 at 13:34
  • 4
    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. – Rob Jan 03 '13 at 15:53
  • 1
    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). – northdig Mar 23 '14 at 01:34
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 =)

Rudie
  • 52,220
  • 42
  • 131
  • 173
  • 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. – Joucks Nov 16 '12 at 15:52
  • Huh... It doesn't work in Firefox. Damnit! So it's not perfect. Or even acceptable. _checking all my old code_ – Rudie Nov 16 '12 at 17:55
  • Another version, but this one doesn't do so well in IE (9). http://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...... – Rudie Nov 17 '12 at 19:52
  • Finally! Just add ` – Mike Keskinov Jul 22 '13 at 22:14
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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • Nope =( No dice. (But that would have been so cool.) http://js2.hotblocks.nl Did I do something wrong? – Rudie May 01 '11 at 22:10
  • @Rudie when I just change the file it works fine - only when clicking the link it denies access - same behavior for you? – Shadow The GPT Wizard May 02 '11 at 06:22
  • @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 May 02 '11 at 09:34
  • @Rudie let me mess with this some more when I'll have time.. keep reminding me in case I forget! – Shadow The GPT Wizard May 02 '11 at 10:03
  • 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. – Rudie May 02 '11 at 10:29
  • @Shadow Did you forget? =) I'm supposed to remind you. I still don't have a decent solution to this hate-IE problem. Did you by any chance (=miracle) find something useful? Gracias – Rudie May 08 '11 at 17:25
  • @Rudie thanks for reminding me... I fear I bring bad news. After extensive trial and error I came to conclusion that IE8 pose a brick wall, probably as part of "security". The only way around I can see is sending the file to the server via AJAX. Ugly, not elegant, but still doable. – Shadow The GPT Wizard May 08 '11 at 19:56
  • @Shadow IE8 doesn't allow file transfer through XHR. That's very modern. I don't want to use Flash or Java for this. So I guess screw IE users. Can you perhaps see a way to detect support for this behaviour that doesn't work in IE? That would be very acceptable =) – Rudie May 08 '11 at 21:12
  • @Rudie I can't see any way to detect such behaviour before it happens - best I can think of it catching the Access Denied error and in there show friendly message like "Something happened, please select file again", hide the upload link and show the ordinary file input control. Also put some flag in a cookie so you will know to show the ordinary file input for that browser. – Shadow The GPT Wizard May 09 '11 at 16:50
  • @Shadow A cookie might be a good idea! I don't like that the user has to repeat the file selection, but if it's only once..... That'll do I guess =( Thanks! **edit** I guess you deserve this +1 =) Thanks for the feedback – Rudie May 09 '11 at 16:52
  • Cheers @Rudie glad I could be at least of *some* help.. :) – Shadow The GPT Wizard May 09 '11 at 22:14
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.

Engin
  • 166
  • 1
  • 4
0

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

  • As you can see on http://js2.hotblocks.nl, I already have: `
    `. The upload actually works in FF and Chrome, so the correct enctype is necessary and present.
    – Rudie May 12 '11 at 19:30