3

I want to styling the file upload form with code below:

<form method='POST' enctype='multipart/form-data' action='file.php'>
<fieldset>
    <legend>Please choose file</legend>
    <input type="file" name="upfile" style="display:none">
    <div class="input-append">
        <input id="filepath" class="input-large" type="text">
        <a class="btn" onclick="javascript:$('input[name=upfile]').click();">Browse</a>
    </div>
    <div>
        <input id="submitBtn" class="btn btn-primary" type="submit" value="Upload">
    </div>
</fieldset>
</form>
<script type="text/javascript">
$('input[name=upfile]').change(function(){
$('#filepath').val($(this).val());
});
</script>

It works perfectly on Chorme/Firefox/Safari but IE 10 it just not fire when I click the submit button.

Any ideas or workaround? Please share with me! thanks!

4
  • Are you seeing error messages in your javascript console? Commented Apr 26, 2013 at 18:35
  • Unfortunately, I didn't see any messages in console.
    – YuLun
    Commented Apr 26, 2013 at 18:39
  • Are you sure nothing is happening when you click submit? Use the network tab in IE to determine if a request was sent or not. You will have to click a button to enable request/response logging in that tab. I can't remember what the button is called offhand. Commented Apr 26, 2013 at 18:48
  • Thanks! I'm sure no data has been sent, and I found out that when I click twice, the submit button just can fire but without file data.
    – YuLun
    Commented Apr 26, 2013 at 19:09

1 Answer 1

2

On my IE10 it does submit, but only after the 2nd click on the submit button. If you use the regular input file instead of a text that will trigger the event, it works (at least for me it did) but I dont know if that's an option for you.

UPDATE:

After some research, I found a solution that might fit your problem:

<style>
  #fileinput { position: absolute; left: -9999em; }
  #link { color: #2a9dcf; font-size: 16px; }
  #link:hover { text-decoration: underline; cursor: pointer; }  
</style>

<form id="uploader-form" method='POST' enctype='multipart/form-data' action='file.php'> 
  <fieldset>
    <legend>Please choose file</legend>
    <div class="input-append">
      <input id="filepath" class="input-large" type="text">
      <input type="file" id="fileinput" />
      <label for="fileinput" id="link" class="trigger-file-input">Browse</label>
    </div>
    <div>
      <input id="submitBtn" class="btn btn-primary" type="submit" value="Upload">
    </div> 
  </fieldset> 
</form>

<script type="text/javascript">
// after the user selects the file they want to upload, submit the form
$('#fileinput').on("change", function() {
    $('#filepath').val($(this).val());
});


// mozilla won't focus a file input with the click of a corresponding
// label, but all other browsers do. if we detect mozilla, listen for
// the label click and send a click to the file input programmatically
if($.browser.mozilla) {
    $('.trigger-file-input').click(function() {
      $('#fileinput').click();                             
    });
}
</script> 

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

4
  • Thanks for your testing. I just found out the same result!
    – YuLun
    Commented Apr 26, 2013 at 19:14
  • I've tried your suggestion, but still not works :'( I am wondering to know why IE10 requires double click to submit in this situation.
    – YuLun
    Commented Apr 27, 2013 at 13:23
  • I tried it on my IE10 and it worked...maybe if you add the header to force the IE9 to render instead.. but it seems like IE won't let you substitute the input file tag for anything else if you want to submit a file..so if looks dont matter to you, it's a 100% solution to just use input file instead.
    – Fabi
    Commented Apr 29, 2013 at 14:51
  • If by Mozilla, you mean Firefox, it seems to work for me. So the last part of your code I didn't need.
    – Peter
    Commented Aug 21, 2013 at 9:36

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.