我正在尝试将文件列表存储在一个全局变量中,以便可以通过链接访问单个文件。我有这个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!“。
在这方面的任何帮助都将非常感谢。这是一项任务,非常紧急。
谢谢!
发布于 2013-03-17 11:34:23
我认为您只是在声明readFiles
之前尝试使用它。
如果要将function readFiles(fileList) {...}
更改为window.readFiles = function (fileList) {...}
,则test
将显示为[object File]
我使用parent.document.getElementById('main').contentWindow.document;
行是因为我认为它不好( it's fire error =)。
发布于 2013-03-17 11:26:59
您的问题出在var doc = parent.document.getElementById('main').contentWindow.document;
上,而不是全局变量。而是您用于文件的作用域。
https://stackoverflow.com/questions/15460122
复制相似问题