javascript - how to clone a file input to another file input which is in hidden iframe - Stack Overflow

i have a form which has a file input element and when the user selects the file i want to set the selec

i have a form which has a file input element and when the user selects the file i want to set the selected file to another file input which is in hidden iframe,

this is what is my form:

<form name="uploadForm" >
    <input type="file" id="fileone" name="fileone" value="" />
    <button type="button" id="attachement_upload" onclick="addFile()" >Upload</button>
</form>
<iframe id="FileUploadFrame" name="FileUploadFrame" src="" style="display: none; border: 0; width: 0; height: 0;">
    <form id="FileUploadForm" name="FileUploadForm" accept-charset="utf-8" target="FileUploadFrame" enctype="multipart/form-data" encoding="multipart/form-data" method="POST" action="CIMtrek_Regional_WhseForm_FileUpload">
        <input type="file" id="filetwo" name="filetwo" value="" />
    </form>
</iframe>

and my java script :

function addFile(){
    //this is where i want to clone the fileone to filetwo and submit the form which is in iframe
}

I have seen some thing like this but really don't know how to do :

$(".inputfield1").change(function(){
  var $this = $(this), $clone = $this.clone();
  $this.after(clone).appendTo(hiddenform);
});

Please help me to get this done.

i have a form which has a file input element and when the user selects the file i want to set the selected file to another file input which is in hidden iframe,

this is what is my form:

<form name="uploadForm" >
    <input type="file" id="fileone" name="fileone" value="" />
    <button type="button" id="attachement_upload" onclick="addFile()" >Upload</button>
</form>
<iframe id="FileUploadFrame" name="FileUploadFrame" src="" style="display: none; border: 0; width: 0; height: 0;">
    <form id="FileUploadForm" name="FileUploadForm" accept-charset="utf-8" target="FileUploadFrame" enctype="multipart/form-data" encoding="multipart/form-data" method="POST" action="CIMtrek_Regional_WhseForm_FileUpload">
        <input type="file" id="filetwo" name="filetwo" value="" />
    </form>
</iframe>

and my java script :

function addFile(){
    //this is where i want to clone the fileone to filetwo and submit the form which is in iframe
}

I have seen some thing like this but really don't know how to do :

$(".inputfield1").change(function(){
  var $this = $(this), $clone = $this.clone();
  $this.after(clone).appendTo(hiddenform);
});

Please help me to get this done.

Share Improve this question asked Feb 8, 2013 at 12:47 Java QuestionsJava Questions 7,97343 gold badges119 silver badges177 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You can't (or shouldn't) be able to do this. The file input type, given that it has direct access to the user's file system, has special security restrictions. Most notably you cannot set the value of a file input in JavaScript, as this would allow you to do things like set it to something like ~/.ssh/id_rsa and steal important data without the user being aware. As a consequence, jQuery shouldn't be able to clone the file input because it can't set the value.

To get this to work you'll have to let the user click on the file input that is in the iframe, perhaps using some CSS positioning tricks to make it seem part of the page. Alternatively, if your users typically have more modern browsers, you could use the File API and upload the file with Ajax.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745523007a4631346.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信