选择多个文件上传<input type="file" id="myfileinput" multiple>

一个FileList对象通常来自于一个HTML input元素的files属性,你可以通过这个对象访问到用户所选择的文件.该类型的对象还有可能来自用户的拖放操作,查看DataTransfer对象了解详情.

Gecko 1.9.2 note
(Firefox 3.6 / Thunderbird 3.1 / Fennec 1.0)

在Gecko 1.9.2之前,通过input元素,每次只能选择一个文件,这意味着该input元素的files属性上的FileList对象只能包含一个文件.从Gecko 1.9.2开始,如果一个input元素拥有multiple属性,则可以用它来选择多个文件.

使用FileList

所有type属性(attribute)为file的 <input> 元素都有一个files属性(property),用来存储用户所选择的文件. 例如:

<input id="fileItem" type="file">

下面的一行代码演示如何获取到一个FileList对象中的第一个文件(File 对象):

var file = document.getElementById('fileItem').files[0];

方法概述

File item(index);

属性

属性名 类型 描述
length integer 一个只读的整数值,用来返回该FileList对象中的文件数量

方法

item()

根据给定的索引值.返回FileList对象中对应的File对象.

File item(
   index
 );
参数
index
File对象在FileList对象中的索引值,从0开始.
返回值

所请求File对象.

示例

这个例子迭代了用户通过一个input元素选择的多个文件:

// fileInput是一个 HTML input 元素: <input type="file" id="myfileinput" multiple>
var fileInput = document.getElementById("myfileinput");

// files 是一个 FileList 对象(类似于NodeList对象)
var files = fileInput.files;
var file;

//遍历所有文件
for (var i = 0; i < files.length; i++) {

    // 取得一个文件
    file = files.item(i);
    // 这样也行
    file = files[i];
    // 取得文件名
    alert(file.name);
}

下面是一个更完整的例子.

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
// multiple属性可以让用户能选择多个文件

<input id="myfiles" multiple type="file">

</body>

<script>

var pullfiles=function(){ 
    // 获取到input元素
    var fileInput = document.querySelector("#myfiles");
    var files = fileInput.files;
    // 获取到所选文件数量 
    var fl=files.length;
    var i=0;

    while ( i < fl) {
        var file = files[i];
        alert(file.name);
        i++;
    }    
}

// 设置change事件处理函数
document.querySelector("#myfiles").onchange=pullfiles;

</script>

</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<tr> <th contenteditable="true" ><input type="text" oninput="filterTable(1)" placeholder="aoiStep" ></th> <th contenteditable="true" ><input type="text" oninput="filterTable(2)" placeholder="defectType" ></th> <th contenteditable="true" ><input type="text" oninput="filterTable(3)" placeholder="layerCode" ></th> <th contenteditable="true" ><input type="text" oninput="filterTable(4)" placeholder="type"></th> <th contenteditable="true" ><input type="text" oninput="filterTable(5)" placeholder="dpet" ></th> <th contenteditable="true"><input type="text" oninput="filterTable(6)" placeholder="subcode" ></th> <th contenteditable="true" ><input type="text" placeholder="codeDescription" ></th> <th contenteditable="true" >image1</th> <th contenteditable="true" >image2</th> <th contenteditable="true">image3</th> <th contenteditable="true" >image4</th> <th contenteditable="true" >image5</th> <th contenteditable="true" ><input type="text" placeholder="determination_rule"></th> </tr> </thead> <tbody> <form action="upload.php" method="POST" enctype="multipart/form-data"> <tr> <td input type="text" name="aoi_step">3</td> <td input type="text" name="defect_type">Particle</td> <td input type="text" name="layer_code">ACT</td> <td input type="text" name="type">Particle</td> <td input type="text" name="dpet">ACT</td> <td input type="text" name="subcode">Particle</td> <td input type="text" name="code_description">ACT</td> <td> <input type="file" name="image1_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> <td> <input type="file" name="image2_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" name="image3_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" name="image4_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" name="image5_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td input type="text" name="determination_rule">Particle</td> <table id="data_table"> <thead> </form> </tbody> </table> 我只需要上传表格内容,不需要上传表头内容,应该怎么处理
07-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值