Open file input dialog and upload onchange possible in IE?

蹲街弑〆低调 提交于 2019-12-03 05:09:08
Rob

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

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

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!