1 Star 0 Fork 0

Blake/js核心梳理

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
19.大文件上传.html 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
Blake 提交于 2024-02-21 12:40 . 第一次提交
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>大文件上传</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
</head>
<body>
<form action="" id="FormUpload">
<p id="percent">上传进度 0%</p>
<input type="file" id="inputUpload" />
<button type="submit">提交</button>
</form>
<script>
const form = document.getElementById('FormUpload')
const ipt = document.getElementById('inputUpload')
let file // 待上传的文件
let uploadPercent = 0 // 上传进度百分比
ipt.addEventListener('change', function (e) {
file = e.target.files[0]
})
form.addEventListener('submit', async function (e) {
e.preventDefault()
let size = 2 * 1024 * 1024 // 设置切片大小 2M
let fileSize = file.size // 文件大小
// 分多少块切片
let totalChunks = Math.ceil(fileSize / size)
for (let i = 0; i < totalChunks; i++) {
let start = i * size
let end = Math.min(start + size, fileSize)
let chunk = file.slice(start, end) // 待上传的切片文件
let formData = new FormData()
formData.append('file', chunk)
formData.append('chunkIndex', i)
formData.append('totalChunks', totalChunks)
// 向后端发送请求
await axios.post('/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
},
//axios提供的监测请求进度的api
onUploadProgress: function (progressEvent) {
uploadPercent = Math.round((progressEvent.loaded / progressEvent.total) * 100)
document.getElementById('percent').textContent = `上传进度 ${uploadPercent}%`
}
})
}
})
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/baiyujie02420/js-core-sorting.git
git@gitee.com:baiyujie02420/js-core-sorting.git
baiyujie02420
js-core-sorting
js核心梳理
main

搜索帮助