我正在尝试将文件列表存储在全局变量中,以便可以通过链接访问单个文件。我有这个 HTML 代码:
<input type="file" id="input" multiple onchange="readFiles(this.files)">
然后在外部 JavaScript 文件中:
theFileList = null;
function test() {
if (theFileList)
alert(theFileList[0]);
else
alert("It is null!");
}
function readFiles(fileList) {
if (fileList) {
// I generate a new page in the "main" frame:
var doc = parent.document.getElementById('main').contentWindow.document;
theFileList = fileList;
test(); // works fine (displays "[object File]").
// opens html and head tags; writes head section (with stylesheet and external js; closes head and opens body:
generateHeader();
// create a button to check if global variable is null:
doc.writeln("<button onclick=\"javascript:test();\">Click</button>");
// closes body and html tags:
generateFooter();
doc.close();
}
else
alert("Something went wrong");
}
如评论中所述,首次加载文件时,测试功能会显示正确的输出。但是,当单击生成的按钮时,输出为“It is null!”。
对此的任何帮助将不胜感激。这是一个任务,非常紧急。
谢谢!