- 论坛徽章:
- 0
|
作者:360weboy
博客:http://www.360weboy.com
发现Firefox, Chrome以及Safari三大浏览器已经支持多文件上传,只需要一个添加一个带一个multiple属性的input就可以实现!
HTML结构
- <!-- 注意: 表单的 enctype属性必须是"multipart/form-data", 这样表单提交的时候,文件数据会被分割成多段-->
- <form method="post" action="upload-page.php" enctype="multipart/form-data">
- <input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
- </form>
复制代码 使用javascript列出选中的文件
- <ul id="fileList">
- <li>暂无文件上传</li>
- </ul>
复制代码- <script type="text/javascript">
- function makeFileList() {
- var input = document.getElementById("filesToUpload");
- var ul = document.getElementById("fileList");
- while (ul.hasChildNodes())
- {
- ul.removeChild(ul.firstChild);
- }
- for (var i = 0; i < input.files.length; i++)
- {
- var li = document.createElement("li");
- li.innerHTML = input.files[i].name;
- ul.appendChild(li);
- }
- if( ! ul.hasChildNodes())
- {
- var li = document.createElement("li");
- li.innerHTML = 'No Files Selected';
- ul.appendChild(li);
- }
- }
- </script>
复制代码 通过input.files属性获取选中的文件,其值是一个数组。所以,我们可以length属性来判断文件数组的长度并且遍历它来得到具体的文件路径以及文件名
![]()
服务器端获取的上传数组格式- Array
- (
- [filesToUpload] => Array
- (
- [name] => Array
- (
- [0] => Armenian_1.1stable.zip
- [1] => Catalan_1.1stable.zip
- )
- [type] => Array
- (
- [0] => application/zip
- [1] => application/zip
- )
- [tmp_name] => Array
- (
- [0] => D:\xampp\tmp\phpA5C7.tmp
- [1] => D:\xampp\tmp\phpA5C8.tmp
- )
- [error] => Array
- (
- [0] => 0
- [1] => 0
- )
- [size] => Array
- (
- [0] => 309810
- [1] => 289961
- )
- )
- )
复制代码 |
|