ie javascript form submit with file input

后端 未结 8 524
-上瘾入骨i
-上瘾入骨i 2020-12-02 17:35

I have a html form, with a custom file upload field. And by that I mean that I have moved the actual file field beyond the borders of the page with css, that I have a custom

相关标签:
8条回答
  • 2020-12-02 17:46

    This is an old post but the problem still arises. This may not be working because jQuery kindly fails silently. I was having this problem and wondering why my hidden form would not submit and the file get uploaded. I started off by using jQuery, but then I went vanilla. It still didn't work but looked as though an exception was being thrown in my .click() function.

    Running

    try {
        document.getElementById('formid').submit();
    } catch (e) {
        alert(e);
    }
    

    showed that we indeed were throwing an error, and quick research showed that this was because IE DOES NOT SUPPORT SIMULATED CLICKS ON A FILE INPUT. This means that when the form went to be posted, IE would refuse to post the form

    Excuse the bold caps, but I know many people will see text and not read it

    0 讨论(0)
  • 2020-12-02 17:51

    If you're like me, and you don't want to use an iframe, and you weren't too keen on the label solution mentioned above, you can just position the original button above the styled button with an opacity of 0.

    Using the example above, you would still have:

    <input type="file" class="hidden" name="hidden" id="hidden" />
    <input type="button" name="shown" id="shown" value="Add File" />
    

    But .hidden would be defined like so:

    .hidden {
      position: absolute;
      left: -150px;
      opacity: 0;
      filter: alpha(opacity=0);
    }
    

    Config: Set the opacity to 0.5 (or =50) to see the transparent element and tweak the left positioning.

    Arguably just as hacky as the answers above, but a bootstrap-friendly solution, and in my case, the only one that worked.

    0 讨论(0)
  • 2020-12-02 17:54

    I was having the same problem, and I solved it by using a styled <label> tag with a slight workaround in Firefox.

    http://jsfiddle.net/djibouti33/uP7A9/

    The Goals:

    1. allow user to upload a file by using standard html file input control
    2. hide standard html file input control and apply own styling
    3. after user selects file to upload, automatically submit the form

    The Browsers:

    • Firefox, Chrome, IE8/9, Safari
    • IE7 didn't work, but it might if you add it to the workaround detailed below.

    The Initial Solution:

    1. Hide the file input by positioning it offscreen. Important not to display:none as some browsers won't like this.
    2. Add another styled element to the page (link, button).
    3. Listen for a click on that element, then programmatically send a click to the file input to trigger the native 'file explorer'
    4. Listen for the file input's onchange event (occurs after a user chooses their file)
    5. Submit the form

    The Problem:

    1. IE: if you programmatically send a click to a file input in order to activate it (2), programmatically submitting the form (5) will throw a security error

    The Workaround Solution:

    1. Same as above
    2. Take advantage of the accessibility features built in to the label tag (clicking on a label will activate it's associated control) by styling a label tag instead of a link/button
    3. Listen for the file input's onchange event
    4. Submit the form
    5. For some reason Mozilla browsers won't activate a file input by clicking on it's label.
    6. For Mozilla, listen for the click on the label and send a click event to the file input to activate it.

    Hope this helps! Check out the jsfiddle for details on the html/js/css used to make it all work.

    0 讨论(0)
  • 2020-12-02 17:57

    You need to change onsumbit='return false;' to onsubmit='return validateForm()'.

    Then have validateForm() return true if the form passes your validation checks, or false if it does not.

    The onsubmit='return false' is preventing the form from submitting via document.thisForm.submit(); as well as when the user clicks the submit button.

    0 讨论(0)
  • 2020-12-02 18:03

    Have you tried

      $('button').click(function(){
            $('form[name=thisForm]').submit()
        });
    
    0 讨论(0)
  • 2020-12-02 18:05

    I commented these lines in j query.form.js then every thing works fine for me. Don't ask me the reason even i don't have the solution for that but it works for sure.

            if (io.contentWindow.document.execCommand) {
              try { // #214
                   io.contentWindow.document.execCommand('Stop');
             } catch(ignore) {}
          }
    
    0 讨论(0)
提交回复
热议问题