<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="file" multiple id="fileInput" />
<button οnclick="dw()">下载</button>
<script>
// 创建一个数组来存储文件的切片
const fileChunks = [];
var name = ''
const fileInput = document.getElementById("fileInput");
fileInput.addEventListener("change", function(event) {
const files = event.target.files;
var f = files[0]
console.log(f);
name = f.name
// 分割文件,大小自定义
var end = 10000
var start = 0
while (start < f.size) {
fileChunks.push(f.slice(start, end))
start = end
end = end + 1000
}
console.log(fileChunks);
});
function dw() {
// 合并转换为文件下载
let blob = new Blob([]);
console.log(blob);
for (var i = 0; i < fileChunks.length; i++) {
blob = new Blob([blob, fileChunks[i]])
}
var url = URL.createObjectURL(blob)
const a = document.createElement('a');
a.href = url;
a.download = name; // 设置下载文件的文件名
a.click(); // 触发下载操作
URL.revokeObjectURL(a.href)
}
</script>
</body>
</html>