3

İ tried to do it simply by assign the files of the input into a variable:

 var files = document.getElementById("upload").files;

but there seems to be a connection created with this assign so every time the input changes the variable changes too. so how can I do that without this connection?

4
  • try an ajax technique, using XMLHttpRequest Commented Oct 30, 2013 at 13:49
  • Why the XMLHttpRequest it's not a server side Commented Oct 30, 2013 at 13:51
  • stackoverflow.com/questions/12530158/… Commented Oct 30, 2013 at 13:52
  • That's not my question thank you anyway Commented Oct 30, 2013 at 13:54

3 Answers 3

3

You just want the filenames? Then just get the filenames :

var files = [],
    upload = document.getElementById("upload");

upload.onchange = function() {
    for (var i=0;i<upload.files.length;i++) {
        files.push(upload.files[i].fileName);
    }
}

??? No "inherited" behaviour from FileList, but I assume I misunderstand.

Sign up to request clarification or add additional context in comments.

1 Comment

No I want the whole file, anyway this method are working fine and the connection gone as well, thank you
1

That's because it's being used as a reference to the files property. If you don't know what that means, do some reading on Google for "pass by value vs pass by reference."

What you need to do to copy the value unfortunately is something like this:

var files = (function() { return document.getElementById("upload").files; })();

In order to copy the value with no reference to the .files property.

The simplistic answer of what is happening here is that var files references the memory address of the files property of that DOM element. It looks to you like it's copying the value when in fact it is pointing to that memory slot and access it is just following a trail to whatever is stored there and accessing it.

1 Comment

The connection still!!
1

I have modified @Mike's answer and came to a result where it actually works. I am writing the answer for a single file which can be converted to support multiple files.

var file = document.getElementById("upload").files[0]

this will store the file and not the refrence to the file hence if the value of upload file type changes the value in file remains unchanged.

Hope this might help someone else

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.