最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery input file click method and access denied on IE - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use only a button as input file, it works fine in Firefox, Chrome, Safari but not in IE... I always get 'access denied' when submitting the form.

The code:

$('#input_file').click();

Is there a real fix for it? I wasted about 2 hours on google but I can't find it.

I'm trying to use only a button as input file, it works fine in Firefox, Chrome, Safari but not in IE... I always get 'access denied' when submitting the form.

The code:

$('#input_file').click();

Is there a real fix for it? I wasted about 2 hours on google but I can't find it.

Share Improve this question edited Aug 31, 2011 at 14:23 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Aug 31, 2011 at 14:11 SoniaSonia 211 silver badge2 bronze badges 10
  • $("#input_file").change() will not open browser dialog – Sonia Commented Aug 31, 2011 at 14:21
  • How about uploadify or similar? – mplungjan Commented Aug 31, 2011 at 14:23
  • Do u have a live version of the form? – Naftali Commented Aug 31, 2011 at 14:25
  • This works for me: jsfiddle/SJWcw. – pimvdb Commented Aug 31, 2011 at 14:25
  • 1 @SoniaL Then perhaps this question is related: stackoverflow./questions/3935001/…. – pimvdb Commented Aug 31, 2011 at 15:03
 |  Show 5 more ments

3 Answers 3

Reset to default 2

I found a way around it. Wrap the button in label tags like this:

<label for="fileinput"><div class="button">Upload file</div></label>


<form>
  <input id="fileinput" name="file" type="file">
</form>

Clicking the label will trigger the file input without invalidating the form in internet explorer (tested in IE9 & IE10)

EDIT: serve this code to IE and keep the javascript solution for other browsers. Firefox will not trigger the file input field when the label is clicked.

I once saw this piece of code, I don't remember where but it is a workaround

  if ($.browser.msie) {
        // IE suspends timeouts until after the file dialog closes
        $flUpload.click(function (event) {
            setTimeout(function () {
                changeFunc();
            }, 0);
        });
    }
    else {
        // All other browsers behave
        $flUpload.change(changeFunc);
    }

Security sandbox "feature" of IE. There is no way to get around it. Sorry. I use the JQuery file input, but still have to detect IE and use the IE basic input, I have spend a lot of time looking for answers and haven't found any. You could use flash, which is what uploadify does to get around the security of IE. I remend it, it's a pretty damn good plug-in.

发布评论

评论列表(0)

  1. 暂无评论