38

So I have two forms, both have a file type input field and I tried

$('.inputfield1').change(function(){
   var file = $(this).val();
   $('.inputfield2').val(file);
});

but then it doesn't get copied properly and firebug complains about "Security Error" in the error console

what did I do wrong and how can I properly copy the value of a file input field

by the way, the destination form has a target that is set to an iframe (not a different domain)

2
  • 3
    Thanks for the question. I was doing the exact same thing and ran into the same problem. Nice to know I'm going where others have already gone. Commented Jan 31, 2013 at 21:49
  • found a solution here stackoverflow.com/a/66306273/819449 Commented Feb 21, 2021 at 19:36

4 Answers 4

40

You can't move the value of one file input to another. Instead, clone the input, place the clone where the original is, and move the original into the hidden form.

$(".inputfield1").change(function(){
  var $this = $(this), $clone = $this.clone();
  $this.after($clone).appendTo(hiddenform);
});
Sign up to request clarification or add additional context in comments.

7 Comments

I know it doesn't, the clone is a placeholder. We are appending the original to the hidden form, .after() returns the original element, not the element we appended after it. Ideally after the iframe loads, we would empty the original and put it back where it belongs.
Where did you get that? I thought the OP only asked how to copy the value of a file input field to another, which cannot be done.
I provided a workaround to a common problem, getting the value of a file input from one file input to one that is in a hidden form, which isn't possible. The workaround is to move the original into the hidden form, submit the form, then move it back. He didn't give us any form code, so I left that out. The clone is for cosmetic purposes only, you don't want a blank spot to show up in the file field's place.
Oh I see the real problem now. Thanks for explaining. +1
I was looking at this code, and when i was trying to do this via a function it was not working as it did not understand what clone was in the line .after(clone)... Instead of it being an on change, i pass the object as a param, so it would be something like $(input) and $clone = $(input).clone();
|
6

If you need to copy the file from one input to another, you could use the below method.

$(".inputfield2")[0].files = $(".inputfield1")[0].files;

Comments

1

I know it is a late answer, but I had a similar problem that I just figured out today.

What I did was move the File input to the new location after deleting the current one in the other location. By moving the element, everything stayed intact on all my tests.

$('.inputfield1').change(function() {
  $('.otherinputfield').remove();
  $('#newform').append($(this));
});

1 Comment

I don't understand, the .otherinputfield sits inside #newform? It will be good to get the snapshot of the html
0
const fileInput = document.querySelector('inputfield2');
const dataTransfer = new DataTransfer();
dataTransfer.items.add($(".inputfield1")[0].files);
fileInput.files = dataTransfer.files;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.