Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ie javascript form submit with file input

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 input field and button in place, and that I have a jquery click event attached to that custom button to trigger the file input dialog. It all works fine, in every browser.

But I need to submit the form through javascript. And I got somewhere that IE remembers my actions with javascript as a malicious manipulation of the file input field and blocks my access with an error "access denied" when I invoke document.formName.submit().

Is there a way around this, because I have gone completely mad by trying to search for a solution. I seriously don't want to use the default file input field, as every browsers renders it differently and messes up my design..

code:

<form name="thisForm" onsubmit="return false;" enctype="multipart/form-data" method="post" action="index.cfm/somepage">
    <input type="file" class="hidden" name="hidden" id="hidden" />
    <input type="text" name="shown" id="shown" />
    <button id="button">browse..</button>
    <input type="submit" id="submitForm" />
</form>

<script>
    $('button').click(function(){
        $('#shown').val($('#hidden').val());
    });

     $('submitForm').click(function(){
        validateForm();
    });

    function validateForm()
    {
        //regular expression validation against all other input fields in the form
        //not the file input field

        validateVAT();
    }

    function validateVAT()
    {
        //connect to external service to check VAT

        submitForm();
    }

    function submitForm()
    {
        document.thisForm.submit();
    }
</script>

UPDATE: I just tried to first upload the file, before submitting the form, through ajax, but that also gave me the acces denied error.. >_>

like image 466
dreagan Avatar asked Feb 22 '12 14:02

dreagan


3 Answers

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.

like image 185
djibouti33 Avatar answered Oct 21 '22 05:10

djibouti33


I found the answer myself, After 2 days of crazy trial&error. I hope I can help somebody with this..

I removed the hidden file input field from my coldfusion page and replaced it by an iframe tag. That iframe tag linked to another coldfusion page, containing another form with the removed file input field. Now when I use javascript to click the file input field, which is still hidden from view, it still gives the browse file dialog without a hitch. But when I use javascript to submit the form, through the iframe, miraculously, it submits the form in the iframe, making it possible to upload the file in some serverside scripting of your preference.

iframe code:

<form id="formFileUpload" class="formFileUpload" name="formFileUpload" method="post" action="../actions/act_upload_file.cfm" autocomplete="off" enctype="multipart/form-data">
    <input type="file" class="buttonFileHidden" id="inputFile" name="partnersLogo" />
</form>

iframe itself:

<iframe src="admin/dsp_file_upload.cfm" id="ifu" name="ifu" class="buttonFileHidden">
</iframe>

javascript click & submit:

ifu.document.formFileUpload.partnersLogo.click();
ifu.document.formFileUpload.submit();
like image 45
dreagan Avatar answered Oct 21 '22 06:10

dreagan


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.

like image 40
Jon Weers Avatar answered Oct 21 '22 05:10

Jon Weers