How to display selected file names before uploadin

2020-02-05 17:20发布

I am using Struts2 to upload multiple files:

<s:file name="files" multiple="multiple" />

When I select multiple files it displays the number of files, eg. 3 files.

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 ?

1条回答
做自己的国王
2楼-- · 2020-02-05 18:21

You can use the HTML5 files property of the <input type="file" /> element like follows:

updateList = function() {
  var input = document.getElementById('file');
  var output = document.getElementById('fileList');

  output.innerHTML = '<ul>';
  for (var i = 0; i < input.files.length; ++i) {
    output.innerHTML += '<li>' + input.files.item(i).name + '</li>';
  }
  output.innerHTML += '</ul>';
}
<input type="file" name="file" id="file" multiple 
       onchange="javascript:updateList()" />
<br/>Selected files:
<div id="fileList"></div>

查看更多
登录 后发表回答