用farmdata上传文件,视频,音频等显示进度

<!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">
    <link rel="stylesheet" href="./form.css">
    <title>Document</title>
    <style>
        .progress{
            width: 800px;
            height: 30px;
            border:1px solid green;
            margin:0 auto;
            overflow:hidden;
            position: relative;
        }

        .in{
            width: 3%;
            height: 100%;
            background: red;
        }

        span{
            position: absolute;
            left:390px;
            top:0;
        }
    </style>
</head>
<body>
    <form id="form1">
        用户名:<input type="text" name="username" >
        密码:<input type="password" name="password" >
        头像:<input type="file" name="myfile" id="pic">
        <input type="button" value="发送ajax请求" id="sub">
    </form>
    <div class="progress" >
        <div class="in"></div>
        <span>0%</span>
    </div>
    <img src="" alt="">
    <script>
        document.querySelector("#sub").onclick = function(){
            var xhr = new XMLHttpRequest();
            xhr.open("post","03-uploadFile.php");
            // 我想自己来设置一个请求头,看行不行--注意了,如果人为了设置了请求头,那么文件数据无法正确的传递
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            // 获取表单数据  new FormData 这种方式不需要设置头部
            var myform = document.querySelector("#form1");
            var formData = new FormData(myform);
            //必须写在send之前 监听上传的进度 onprogress 上传的过程中会一直触发的 事件函数 如果文件大会很多次
            xhr.upload.onprogress=function(e){
                // e就有当前文件上传的过程中的一些数据
                // console.log(e);
                // e.loaded  当前上传了多大   e.total 总共有多大
                var num=Math.floor((e.loaded/e.total)*100);
                var percent=num+"%";//12.22
                console.log(percent)
                document.querySelector(".in").style.width=percent;
                document.querySelector(".progress span").innerHTML=percent;
            }

            xhr.send(formData);
            xhr.onreadystatechange = function(){
                if(xhr.status == 200 && xhr.readyState == 4){
                    console.log(xhr.responseText);
                }
            }
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值