通过ES6 封装了一个上传文件的方法 XMLHttpRequest() 通用
### 上传进度回显,上传速度回显
### 源码如下,新建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() 通用的更多相关文章
- 朋友封装的一个ASP.NET上传文件的方法
朋友做了asp.net开发多年,做了这个,自我感觉封装得还不错!!! 代码如下: #region 上传文件的方法 /// <summary> /// 上传文件方法 /// </sum ...
- MUI上传文件的方法
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 以一个上传文件的例子来说 DistributedFileSystem
public class UploadAndDown { public static void main(String[] args) { UploadAndDown uploadAndDown = ...
- web 中常用的两种上传文件的方法总结
这里我们来总结整理一下常用的两种文件上传方式以及要注意的东西: 1.springmvc .MultipartFile 的上传方式. 2.org.apache.commons.fileupload 使用 ...
- IE input file隐藏不能上传文件解决方法
当大神们都在探讨更深层次的问题时,我还在这里转载发些肤浅的问题解决方案.罢了,为了和我一样笨的后来人. 问题: 上传文件时,用<input type="file" /> ...
- 使用jquery插件uploadify上传文件的方法与疑问
我是学生一枚,专业也不是计算机,但又要用到很多相关技术,所以在技术基础不牢靠的情况下,硬着头皮在做.最近在做一个小项目需要上传图片,而且是需要用ajax的方式.但是利用jquery的ajax方法总会有 ...
- Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法
记录瞬间 近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错. 比如: 1.Current request is not a m ...
- uni-app开发的应用(小程序,app,web等),使用Node+Koa2开发的后端程序接收上传文件的方法
uni-app使用使用Node+Koa2开发的后端程序接收上传的文件 通过gitbook浏览此随笔 通过其它客户端上传(h5,小程序等),接收方法一致 使用koa接收时,我们需安装一个中间件koa-b ...
- python通过http(multipart/form-data)上传文件的方法
之前写过一篇博客,说的如何python如何通过http下载文件,今天写一篇博客来介绍如下,python如何通过request库实现上传文件 这里主要是解决multipart/form-data这种格式 ...
随机推荐
- SVPWM总结
空间矢量算法 是以逆变器和电机作为一个整体来研究的.目标是产生电机定子的圆形磁场 模态选择, 上管导通 状态为1 下管导通 状态为0 那么状态为000 001 010 011 100 101 110 ...
- 安装部署及升级到Exchange Server 2010
本文档详细的描述了,如何在Windows Server 2008 R2的环境下安装Exchange Server 2010,包括的内容有: 先检查组织环境: 1.请确保林的功能级别至少为 Wind ...
- 19 02 03 django 中cookies 和 session 和 cache
Session 是单用户的会话状态.当用户访问网站时,产生一个 sessionid.并存在于 cookies中.每次向服务器请求时,发送这个 cookies,再从服务器中检索是否有这个 session ...
- Power BI Desktop 安装环境
Power BI Desktop 环境 最低要求 Windows 7/Windows Server 2008 R2 或更高版本 .NET 4.5 Internet Explorer 9 或更高版本 内 ...
- javaweb利用ajax使登录窗口不跳转页面实现对账号密码的判断
和上一篇判断用户名是否被占用不跳转页面类似!利用ajax实现跳转,要导入jquery文件库!具体代码我会贴出来,注释在里面!!可以观摩一手!(代码我也留下链接,如果失效,评论补发,代码可能导入也无法使 ...
- tornado反向解析
tornado反向解析 在路由中添加name属性,并且不能使用元组路由,应当由tornado.web.url定义路由. app = tornado.web.Application([ (r'/', I ...
- 使用Linux系统,是一种什么体验?
导读 同事,从事嵌入式软件开发多年,主要开发环境用的就是linux,最疯狂的一段时间直接把系统装成linux系统,然后在linux下面虚拟一个windows操作系统,主要有些事情必须在windows才 ...
- oracle数据库常用操作语句 、创建视图
新增字段:alter table 表名 add (NAME VARCHAR(12), NAME NUMBER(10) );--如果添加单个字段可以不用括号包起来,例如 alter table cust ...
- org.apache.jasper.JasperException: /WEB-INFO/jsp/product/edit.jsp(168,45)
PWC6038:"${empty data.code?'001':fn:substring(data.code,0,8)}" contains invalid expression ...
- LeetCode简单题汇总
1.两个数之和 给出一个整数数组,请在数组中找出两个加起来等于目标值的数, 你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index ...