1 Star 0 Fork 4

叾屾/tool-box

forked from sohucw/tool-box 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
file.html 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
chenjianwei01 提交于 2022-06-14 11:38 . feat: 更新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>upload文件上传</title>
<style>
.box{height:20px;width:200px;margin-top:20px;}
#progress{background:red;height:20px;border:1px solid #ccc;width:0;}
</style>
</head>
<body>
<h2>请选择要上传的文件:</h2>
<input type="file" id="file">
<div class="box">
<div id="progress"></div>
</div>
<h1 style="color:yellow;"></h1>
</body>
<script>
function $(select){
return document.querySelector(select);
}
$("#file").onchange = function(event){
let file = event.target.files[0]; // 获取file对象
debugger;
let formData = new FormData(); // 新建formData对象
formData.append('file', file); // 将file添加到formData传到后端
let xhr = new XMLHttpRequest(); // 新建ajax异步对象
xhr.setRequestHeader('Content-Type', 'multipart/form-data' );
xhr.open('post', '/'); // 打开ajax
// 监听ajax对象事件状态
xhr.onreadystatechange = function (){
if(xhr.readyState == 4 && xhr.status == 200){
$('h1').innerText = '上传成功!';
}
}
// 监听文件上传进度
xhr.upload.onprogress = function (ev){
if(ev.lengthComputable){
console.log(1);
let progress = ev.loaded/ev.total *100;
$('#progress').style.width = progress + '%';
$('#progress').innerText = progress + '%';
}
};
xhr.send(formData); // 发送ajax请求将formData对象发送过去
}
</script>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaobo1012/tool-box.git
git@gitee.com:xiaobo1012/tool-box.git
xiaobo1012
tool-box
tool-box
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385