javascript 如何在Struts2中上传多个文件之前显示选定的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26082721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display selected file names before uploading multiple files in Struts2?
提问by Pradnya
I am using Struts2 to upload multiple files:
我正在使用 Struts2 上传多个文件:
<s:file name="files" multiple="multiple" />
When I select multiple files it displays the number of files, eg. 3 files.
当我选择多个文件时,它会显示文件数,例如。3个文件。
The project requirements are that the user should be able to see what files he selected before uploading.
项目要求是用户在上传之前应该能够看到他选择了哪些文件。
Is it possible to display the selected files names in a list or maybe in the control itself ?
是否可以在列表中或在控件本身中显示选定的文件名?
回答by Andrea Ligios
You can use the HTML5 files
property of the <input type="file" />
element like follows:
您可以使用元素的 HTML5files
属性,<input type="file" />
如下所示:
updateList = function() {
var input = document.getElementById('file');
var output = document.getElementById('fileList');
var children = "";
for (var i = 0; i < input.files.length; ++i) {
children += '<li>' + input.files.item(i).name + '</li>';
}
output.innerHTML = '<ul>'+children+'</ul>';
}
<input type="file" multiple
name="file"
id="file"
onchange="javascript:updateList()" />
<p>Selected files:</p>
<div id="fileList"></div>