tags:

views:

21

answers:

2

I use the jquery iframe plugin to squirt HTML into my iframe. Amongst other things, it contains:

<form id="update" action="dummy" method="POST" enctype="multipart/form-data">
     <div><input type="file" name="data" id="update_data"></div>
</form>

Then I run the following function. Watching the fun in the Chrome debugger, I can see that I am successfully latching onto the frame, form, and input field. But the picker does not pop up, and the form does not submit to the (JAX-RS) service.

function update_file(file) {
    var iframe = $('#new_file_iframe');
    var delform = iframe.contents().find("#update");
    var fileinput = iframe.contents().find("#update_data");
    // URLencoding?
    delform.attr("action", "/rex-ws/service/jape/update-file/" + file);
    fileinput.click();
    delform.submit();
}

Perhaps click is not the right thing to set it off?

+1  A: 

This is a security issue. Browsers are notoriously stingy with what may be done to a file upload programmatically.

I'm pretty sure running fileinput.click() is out of the question for this reason.

Flash-based uploaders like Uploadify and SWFUpload have more liberties here. I think it is possible to open the file picker programmatically with them. However, using one of these uploaders would require some work.

Pekka
+1  A: 

I've done this in an project, I'll tell you it's a bit tricky.

What you basically have (Well at least the why I did) to do is:

  • Have a input[file] in your main page
  • On your upload action (ie an click event on the main page) create and iframe with a form and duplicate your input[file] and attach it to your newly created iframe form
  • Submit the form in the iframe with ajaxSubmit (jQuery plugin)

If you would like I could add the code here (but it's about 120 rows).

..fredrik

Edit:

If you are designing a HTML5 site, have a look at the new input file API. https://developer.mozilla.org/en/using_files_from_web_applications

fredrik
I don't see how this opens the file selection dialog automatically. Also, I'm pretty sure the file input won't survive the transport into the iframe with a file still selected. Are you sure this works?
Pekka
Well, the popup won't open up automaticly. It'll be a user action. It's just another approach to the same problem (Don't think that's the issue here?!). But yes the input will survive. Since the input is only a DOM element with properties that can be manipulated until the form is submitted. It took while to figure everthing out, but in short it works.
fredrik
@fredrik but isn't the automatic click()ing the OP's point? (I can be wrong, though.)
Pekka
@Pekka: now i'm getting a bit unsecure about if it's the automatic popup or the submit that's the problem.@bmargulies: is it both or just the one that's the issue?
fredrik