I'm trying to achieve ajax-like-file-upload-using-hidden-iframe. Obviously there are many plugins available for that purpose, but I think my requirements are a bit different. So I'm trying to achieve it as follows:
HTML:
<!DOCTYPE>
<html>
<style>
.file_upload{cursor:pointer;color:blue;text-decoration:underline;}
</style>
<body>
<div class="file_upload" id="upload1" name="doc1" >Upload doc1</div>
<div class="file_upload" id="upload2" name="doc2" >Upload doc2</div>
<div id="iframe_div"></div>
</body>
</html>
JQUERY:
$(document).ready(function(){
$(".file_upload").click(function(event){
var itr = $(".file_upload").length;
var iframeName = $(this).attr('name') + itr;
var iframeId = $(this).attr('id') + itr;
$('<iframe />', {
name: iframeName,
id: iframeId,
src: 'about:blank'
}).appendTo('#iframe_div');
//append form to body of the iframe
$('<form>',{
name: iframeName + '_form',
id: iframeId + '_form',
action: 'actionName.action',
method: 'POST',
enctype: 'multipart/form-data'
}).appendTo('#' + iframeId).find('body');
//append file input
//trigger 'click' using $('#file_fld_id').trigger('click')
//browse to file and use 'onchange' to trigger
auto-submit of the form
//replace the 'file-upload' div with a progress bar...
//remove the iframe when upload is complete
});
});
In Short:
1. Open browse window onclick of a div.
2. Upload the file using hidden iframe.
But I think I'm too knowledgeable(in jquery) to achieve this. Or is it really possible?? If so please help/redirect/anything.....how come this obvious functionality has to be implemented with work-arounds(flash,obex,silverlight,....)..ahhh.
EDIT(to clarify a bit):
As I mentioned 'my iframe is dynamic'...the reason is that my file inputs are supposed to be inside an already existing form with a separate action(struts2). I've got yet another action for file upload. As we can't have form inside a form, so what I'm trying is to use a div inside my existing form for file uploading with the other action. And I've got different docs to be uploaded separately within this form. So basically I'm trying to replace each div(one being used for file upload) with a separate iframe having its own form(but same action would handle the upload) which will get auto-submitted the moment user clicks 'open' while browsing the file. I hope I'm clearer now.