html页面上传进度条

进度条样式

    #progress-bar {
        width: 500px;
        height: 20px;
        background: #cccccc;
        border-radius: 5px;
    }
    #progress-warp {
        width: 0px;
        height: 20px;
        background: #1B7EE0;
        border-radius: 5px;
    }
    #progress-precent {
        width: 100%;
        height: 100%;
        line-height: 20px;
        font-size: 14px;
        color: #FFFFFF;
        text-align: center;
    }

进度条

<div id="progress-bar">
    <div id="progress-warp"><div id="progress-precent"></div></div>
</div>
<div id="upload-result"></div>
<input type="file" accept=".mp4,.docx" onchange="uploadFile(this)">

js方法

function uploadFile(obj) {
    var file = obj.files[0];
    var fileSize = file.size;  //大小为b
    if (fileSize > 1024 * 1024 * 100) {//100M
        alert("上传文件不能超过100M");
        obj.value = '';
        return false;
    }
    if (file.type != 'audio/mp4' && file.type != 'video/mp4' && file.type != 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
        alert("文件上传格式不正确");
        obj.value = '';
        return false;
    }    
    var formData = new FormData();
    formData.append("file", file);
    //后台接口地址
    var url = locat_file + "/uploadfile";
    var xhr = new XMLHttpRequest();
    //加token
    //xhr.setRequestHeader('token',sessionStorage.getItem('token'));
    xhr.open('post', url, true);
    xhr.onreadystatechange = function () {
        //readystate为4表示请求已完成并就绪
        if (this.readyState == 4) {
            //在进度条下方显示接口返回结果
            document.getElementById('upload-result').innerHTML = this.responseText;
        }
    };
    xhr.upload.onprogress = function (ev) {
        //如果ev.lengthComputable为true就可以开始计算上传进度
        //上传进度 = 100* ev.loaded/ev.total
        if (ev.lengthComputable) {
            var precent = 100 * ev.loaded / ev.total;
            //更改进度条,及百分比
            document.getElementById('progress-warp').style.width = precent + '%';
            document.getElementById('progress-precent').innerHTML = Math.floor(precent) + '%';
        }
    }
    xhr.send(formData);
}

后台

    /**
     * 单文件上传
     *
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/uploadfile")
    public R uploadfile(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        if (file != null) {
            R r = bladeFileService.saveFile(file, request);
            return r;
        } else {
            return R.error("缺失参数");
        }
    }

页面效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunyanchun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值