首页 技术 正文
技术 2022年11月15日
0 收藏 475 点赞 4,827 浏览 2954 个字

### 上传进度回显,上传速度回显

### 源码如下,新建index.js装起来

export class UploadServers {
constructor (options) {
this.xhr = null
this.startTime = null
this.startFileSize = 0
this.formData = null
// this = options
Object.keys(options).map(item => {
this[item] = options[item]
})
this.init(options)
}
init (options) {
this.config = {...this.config, ...options}
this.xhr = new XMLHttpRequest()
this.formData = new FormData()
if (this.config.data && Object.prototype.toString.call(this.config.data) === '[object Object]') {
// 循环添加其他参数
this.config.data.keys(item => {
this.formData.append(item, this.config.data[item])
})
}
// console.log(this.config)
// console.log(this.config.file.toString())
// console.log(Array.prototype.slice.call(this.config.file).toString())
if (this.config.file.toString() === '[object FileList]' || this.config.file.toString() === '[object File]' || this.config.file.toString() === '[object Array]' || this.config.file.toString().includes('[object File]')) {
this.uploadFile(this.config.file, true)
} else {
this.uploadFile(this.config.file)
}
}
uploadFile (file, isArray) {
// this.xhr
const _this = this
if (isArray) {
Object.values(file).forEach(function (item) {
_this.formData.append(_this.config.uploadFileName, item)
})
} else {
this.formData.append(this.config.uploadFileName, file)
}
this.xhr.open('post', this.config.url, true)
this.xhr.onload = function (e) {
_this.updataSucess(e)
}
this.xhr.onerror = function (e) {
_this.updataError(e)
}
this.xhr.upload.onprogress = function (e) {
_this.progressChange(e)
}
this.xhr.upload.onloadstart = function (e) {
_this.startUpload(e)
}
this.xhr.send(this.formData)
}
startUpload (e) {
// console.log(e)
this.startTime = new Date().getTime()
this.startFileSize = 0
}
updataSucess (e) {
// console.log(e)
// console.log(this)
// console.log(uploadServers)
this.config.success(e)
}
updataError (e) {
console.log(e)
this.config.error(e)
}
progressChange (e) {
// console.log(e)
if (e.lengthComputable) {
const newTime = new Date().getTime()
let pertime = (newTime - this.startTime) / 1000
// 如果时间为0 则返回避免出现Infinity 兼容IOS进度函数读取过快问题
if (pertime === 0) pertime = 0.001
this.startTime = newTime const perload = e.loaded - this.startFileSize
const lave = e.loaded / e.total
this.startFileSize = e.loaded let speed = perload / pertime
// console.log(perload, pertime)
// const speeds = speed let units = 'b/s'
if (speed / 1024 > 1) {
speed = speed / 1024
units = 'k/s'
}
if (speed / 1024 > 1) {
speed = speed / 1024
units = 'M/s'
}
if (speed / 1024 > 1) {
speed = speed / 1024
units = 'G/s'
}
// console.log(speed)
speed = speed.toFixed(1)
// console.log(speed)
// const resout = ((e.total - e.loaded) / speeds).toFixed(1) this.config.progress(e, speed, lave, e.loaded, units)
}
}
}

使用方式

      let initUploadFileChange = new UploadServers({
url: _this.url,
data: _this.data,
file: fileList || null,
fileClassName: null,
uploadFileName: _this.fileOption || 'multipartFiles',
progress: function (e, speed, lave, loaded, units) {
// console.log(e, speed, lave, loaded, units)
_this.percentage = parseInt(lave * 100)
},
success: function (e) {
if (e.target.status === 200 && e.target.response) {
const parseJson = JSON.parse(e.target.response)
}
// 设置状态为未上传状态
_this.processStatus = false
},
error: function (e) {
// 上传失败 应将文件通过File流读取出来进行回显 并展示给用户提示上传失败 请重新上传 或者自动重新上传
_this.processStatus = false
}
})

####

####

END

####

####

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898