最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML: select multiple files but no file names shown? - Stack Overflow

programmeradmin4浏览0评论

I am using "multiple" tag to let user choose multiple files for uploading.

<input type="file" name="files[]" multiple />

But it only shows e.g., "3 files selected". It would be better to also show file names.

Is it doable? Thanks.

BTW, in this case how to deselect files (e.g., clear the list)?

I am using "multiple" tag to let user choose multiple files for uploading.

<input type="file" name="files[]" multiple />

But it only shows e.g., "3 files selected". It would be better to also show file names.

Is it doable? Thanks.

BTW, in this case how to deselect files (e.g., clear the list)?

Share Improve this question edited Jan 29, 2014 at 23:21 Joeytje50 19.1k15 gold badges68 silver badges99 bronze badges asked Jan 29, 2014 at 21:18 user180574user180574 6,11416 gold badges64 silver badges106 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can do this with the .files property on the input element:

document.getElementById('files').addEventListener('change', function(e) {
  var list = document.getElementById('filelist');
  list.innerHTML = '';
  for (var i = 0; i < this.files.length; i++) {
    list.innerHTML += (i + 1) + '. ' + this.files[i].name + '\n';
  }
  if (list.innerHTML == '') list.style.display = 'none';
  else list.style.display = 'block';
});
<input type="file" id="files" multiple />
<pre id="filelist" style="display:none;"></pre>

This first empties the list in case there's still something from the previous selection, and then it goes through each item in the file list, and then adds that to the <pre> element underneath the input. The <pre> will be hidden if there are no selected items in the list.

PS: you can clear the list by just clicking the input, and then either pressing esc or clicking "cancel" on the file-selection window.

发布评论

评论列表(0)

  1. 暂无评论