后台系统上传文件回显上传进度条

本文介绍了如何在Angular4项目中利用XMLHttpRequest Level 2的特性实现后台文件上传进度条。通过监听xhr.readyState === 3的状态获取上传进度,并在服务器端通过flush()方法分批返回数据,实现前端进度的实时更新。虽然xhr.upload.progress可跟踪上传进度,但在本文的场景中,服务器的处理方式使得progress事件并不适用。
摘要由CSDN通过智能技术生成

 

大家有必要看到文末

xhr传统的AJAX传输对象,在做后台系统的时候经常遇到文件上传的情景,以往的xhr已经能够应对文件表格上传的功能,但如今遇到项目需求,上传的文件比较大数据表较多,为了提高用户体验有必要增加文件上传进度条,让用户知悉当前的操作进度,以免一直停留在空白页究竟有没有上传服务器都不清楚。此文用到的也还是AJAX的技术,XMLHttpRequest Level 2特性(也是老东西)

XHR2的事件例表:

attribute type Explanation
onloadstart loadstart When the request starts.
onprogress progress While loading and sending data.
onabort abort When the request has been aborted, either by invoking the abort() method or navigating away from the page.
onerror error When the request has failed.
onload load When the request has successfully completed.
ontimeout timeout When the author specified timeout has passed before the request could complete.
onloadend loadend When the request has completed, regardless of whether or not it was successful.

 

回顾AJAX基本知识

ajax基本结构

此文要做进度条,必须要在xhr.readyState === 3(解析,XMLHttpRequest对象开始持续读取服务器的响应)的状态中进行获取进度

 

<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值