获取文件的信息,并排序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Upload Files</title>
</head>
<body>
    <input type="file" id="filePicker" multiple>
    <button onclick="listFiles()">List Files</button>
    <label>Sort By:</label>
    <select id="sortBy" onchange="listFiles()">
        <option value="name">Name</option>
        <option value="size">Size</option>
        <option value="time">Create Time</option>
    </select>
    <pre id="fileList"></pre>

    <script>
        // 比较函数:根据不同的属性进行排序
        function compare(a, b, property) {
            if (property === "size") {
                return a[property] - b[property];
            } else if (property === "name") {
                return a[property].localeCompare(b[property]);
            } else if (property === "time") {
                return Date.parse(a[property]) - Date.parse(b[property]);
            }
        }

        // 获取所有选择的文件信息,并按照选择的属性进行排序
        function listFiles() {
            var fileList = document.getElementById('fileList');
            fileList.innerHTML = "";

            var files = document.getElementById('filePicker').files;

            var fileInfos = [];

            for (var i = 0; i < files.length; i++) {
                var fileInfo = {};
                fileInfo["Name"] = files[i].name;
                fileInfo["Size"] = files[i].size;
                fileInfo["Type"] = files[i].type;
                fileInfo["CreateTime"] = files[i].lastModifiedDate.toLocaleDateString();

                fileInfos.push(fileInfo);
            }

            // 获取选择的排序属性
            var property = document.getElementById("sortBy").value;

            // 根据选定属性对文件信息进行排序
            fileInfos.sort(function(a, b) {
                return compare(a, b, property);
            });

            // 将文件信息以 JSON 形式输出
            fileList.innerHTML = JSON.stringify(fileInfos, null, "\t");
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值