admin管理员组文章数量:1303968
İ 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?
İ 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?
Share Improve this question edited Mar 28, 2020 at 23:51 Tarek.hms asked Oct 30, 2013 at 13:33 Tarek.hmsTarek.hms 1,2831 gold badge10 silver badges15 bronze badges 4- try an ajax technique, using XMLHttpRequest – jamesTheProgrammer Commented Oct 30, 2013 at 13:49
- Why the XMLHttpRequest it's not a server side – Tarek.hms Commented Oct 30, 2013 at 13:51
- stackoverflow./questions/12530158/… – jamesTheProgrammer Commented Oct 30, 2013 at 13:52
- That's not my question thank you anyway – Tarek.hms Commented Oct 30, 2013 at 13:54
3 Answers
Reset to default 3You 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.
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.
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
本文标签: formsHow to save the file input data to a variable in javascriptStack Overflow
版权声明:本文标题:forms - How to save the file input data to a variable in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741779346a2397214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论