HTML DOM Input FileUpload files 属性

htmlweb developmentfront end technology更新于 2025/6/28 14:04:17

HTML DOM FileUpload files 属性返回一个 FileList 对象,该对象包含文件上传按钮选中的所有文件。

语法

以下是语法 −

object.files

示例

让我们看一个 FileUpload files 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   body{
      text-align:center;
      background-color:#52B2CF;
      color:#fff;
   }
   .btn{
      background-color:coral;
      border:none;
      height:2rem;
      border-radius:50px;
      width:60%;
      margin:1rem auto;
      display:block;
      color:#fff;
      outline:none;
   }
   .show{
      font-size:1.2rem;
      color:#fff;
      font-weight:bold;
   }
</style>
</head>
<body>
<h1>DOM FileUpload files Property Example</h1>
<p>Select some files</p>
<input type="file" class="file-upload-btn" multiple>
<button onclick = "showFiles()" class = "btn">Click me to see all selected file</button>
<div class="show"></div>
<script>
   function showFiles() {
      var fileBtn = document.querySelector(".file-upload-btn");
      var showMsg = document.querySelector(".show");
      showMsg.innerHTML='';
      if(fileBtn.files.length === 0){
         showMsg.innerHTML = "Please select at least one file!!";
      } else {
         showMsg.innerHTML = "You selected these files:";
         for(let i=0;i<fileBtn.files.length;i++) {
            showMsg.innerHTML += '<p>' + fileBtn.files[i].name + '<p>';
         }
      }
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"点击我查看所有选定的文件"按钮显示所有选定的文件。


相关文章


有用资源