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

### 源码如下,新建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

####

####

通过ES6 封装了一个上传文件的方法 XMLHttpRequest() 通用的更多相关文章

  1. 朋友封装的一个ASP.NET上传文件的方法

    朋友做了asp.net开发多年,做了这个,自我感觉封装得还不错!!! 代码如下: #region 上传文件的方法 /// <summary> /// 上传文件方法 /// </sum ...

  2. MUI上传文件的方法

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 以一个上传文件的例子来说 DistributedFileSystem

    public class UploadAndDown { public static void main(String[] args) { UploadAndDown uploadAndDown = ...

  4. web 中常用的两种上传文件的方法总结

    这里我们来总结整理一下常用的两种文件上传方式以及要注意的东西: 1.springmvc .MultipartFile 的上传方式. 2.org.apache.commons.fileupload 使用 ...

  5. IE input file隐藏不能上传文件解决方法

    当大神们都在探讨更深层次的问题时,我还在这里转载发些肤浅的问题解决方案.罢了,为了和我一样笨的后来人. 问题: 上传文件时,用<input type="file" /> ...

  6. 使用jquery插件uploadify上传文件的方法与疑问

    我是学生一枚,专业也不是计算机,但又要用到很多相关技术,所以在技术基础不牢靠的情况下,硬着头皮在做.最近在做一个小项目需要上传图片,而且是需要用ajax的方式.但是利用jquery的ajax方法总会有 ...

  7. Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法

    记录瞬间 近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错. 比如: 1.Current request is not a m ...

  8. uni-app开发的应用(小程序,app,web等),使用Node+Koa2开发的后端程序接收上传文件的方法

    uni-app使用使用Node+Koa2开发的后端程序接收上传的文件 通过gitbook浏览此随笔 通过其它客户端上传(h5,小程序等),接收方法一致 使用koa接收时,我们需安装一个中间件koa-b ...

  9. python通过http(multipart/form-data)上传文件的方法

    之前写过一篇博客,说的如何python如何通过http下载文件,今天写一篇博客来介绍如下,python如何通过request库实现上传文件 这里主要是解决multipart/form-data这种格式 ...

随机推荐

  1. Spring入门之三-------SpringIoC之Scopes

    一.singleton和prototype public class Bean1 { public Bean1() { System.out.println(this.getClass().getSi ...

  2. HDU - 1166 敌兵布阵 (线段树---点修改)

    题意: 1.Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30) 2.Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30); 3.Query i j ,i和j ...

  3. MYSQL 免安装版修改root密码

    c:>mysql –uroot; mysql>show databases; mysql>use mysql;//不用修改 mysql>UPDATE user SET pass ...

  4. 指令——free

    free指令 一个完整的指令的标准格式: Linux通用的格式——#指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个. free指令作用:查看内存使用情 ...

  5. error LNK2019: 无法解析的外部符号……

    在VS中开发程序的时候遇到一个问题,应该算是比较常见,所以记录下. 在编译程序的时候遇到一个错误,大致提示如下: "error LNK2019: 无法解析的外部符号--" 遇到这个 ...

  6. 一百零一、SAP中ALV事件之十四,让ALV表格自动排序

    如果我们需要对下图的凭证日期和物料进行排序,需要怎么做呢 一.我们来到ALV的定义 二.我们查看IT_SORT的定义,双击点进去 三.查看SLIS_T_SORTINFO_ALV定义 四.代码如下,定义 ...

  7. C#中类的字段或属性不被序列化成JSON或XML

    将一个类序列化成JSON或XML时,如果某个字段或属性不想被序列化,则可以使用以下Attribute: 1.[Newtonsoft.Json.JsonIgnore]特性:使用Newtonsoft.Js ...

  8. 通过request获得全路径

     <% String test = request.getScheme()+"://"+request.getServerName()+":"+reque ...

  9. 第二篇MTV模型、基本命令、简单配置

    MTV模型.基本命令.简单配置 阅读目录(Content) MTV模型 基本命令 简单配置 MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Tem ...

  10. C语言拾遗——sscanf

    今天写题用到了sscanf,怕忘赶紧记录一下 去百度了一下这玩意的函数原型好像是长这样的,微软上扣下来的  int sscanf( const char *buffer, const char *fo ...