1

I have a iframe inside a main page. The when a button(inside form1) is clicked inside the iframe, it makes a AJAX call and the response html will be a input type file and input type button inside a form(say form2) and the response will be put in a div to show it as a popup over the normal screen. Using the input type file is user is able to select a file and when button is clicked it will call a function to submit the form and the file wil be uploaded to server.

The requirement is to programmatically click the input type file and display the file open dialog. When the user selects a file, we need to programmatically click the button to submit to the server.

I tried to programmatically click the input type file through javascript from the main page after AJAX response is populated. User is able to select file but programmatically click the button results in "Access is denied" error in IE.

I do not use any js library. Do i need to use any additional library to achieve this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
rsnagaraj6in
  • 45
  • 1
  • 8
  • There are interesting answers to this question in here: http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input-e/3030174#3030174 – Philippe Plantier Sep 10 '11 at 16:50
  • The iframe and the main page are in different domains? – Ricardo Bin Aug 29 '11 at 12:41

1 Answers1

1

The requirement is to programmtically click the input type file and display the file open dialog.

This requirement cannot be reliably fulfilled. Some browsers will respond to click() on an input type="file", others won't, others (IE) appear to permit it but then refuse to actually submit the form.

There is no workaround and no library can avoid the limitation—short of going to some non-HTML means of uploading a file such as Flash or Java.

What's more there is nothing in the HTML standard that requires there to even be a file open dialog. This is a browser- and OS-specific interface detail. Other platforms than Windows desktop browsers may have other mechanisms for submitting files, such as drag and drop. You can't meaningfully interact at this level with a file upload field.

When the user selects a file, we need to programmtically click the button to submit to the server.

Well that bit's doable. Catch the change event:

<form method="post" action="..." enctype="multipart/form-data">
    <input type="file" id="f"/>
</form>

document.getElementById('f').onchange= function() {
    if (this.value!=='')
        this.form.submit();
};
bobince
  • 528,062
  • 107
  • 651
  • 834
  • [Works for me.](http://jsfiddle.net/ZmSqy/). And yes, file upload fields and files are both read-only for obvious security reasons. – bobince Aug 31 '11 at 21:52
  • The form is got via ajax response. doesnt work with above code – rsnagaraj6in Sep 16 '11 at 07:07
  • Make sure your script is already in place, and run the `onchange` binding after you have loaded the `
    ` into the page. You can't load `
    – bobince Sep 16 '11 at 09:15
  • can u clarify on the above. I click a 'Attach' button, this makes ajax request. response contain a form and other file upload elements. user should click browse, select file and click upload button. This is how the erp system work. I have no control over the file upload server code. I tried putting on change event in the ajax response and then i put the response in dom. Now, i click browse and select file, the form is submitted successfully. what i dont want to click browse. One click on Attach button, file browse to be displayed. file select should submit form. reducing the clicks is wanted – rsnagaraj6in Sep 16 '11 at 09:48
  • As I said in the answer, you cannot automatically ‘click browse’. It won't work in many browsers, it breaks the form in IE, and there's not even necessarily a Browse button there to click. Sorry, but this is just not possible within HTML file upload controls. As for attaching the `onchange` event, you must do that in your AJAX-response handling code, just after you have added the file upload control to the page. – bobince Sep 16 '11 at 11:18