您现在的位置是:网站首页> 编程资料编程资料
Html5 new XMLHttpRequest()监听附件上传进度html5的pushstate以及监听浏览器返回事件的实现Html5监听手机摇一摇事件的实现html5中监听canvas内部元素点击事件的三种方法html5 postMessage前端跨域并前端监听的方法示例详解Html5 监听拦截Android返回键方法Html5 APP中监听返回事件处理的方法示例使用html5新特性轻松监听任何App自带返回键的示例一个不错的HTML5 Canvas多层点击事件监听实例
2023-10-11
320人已围观
简介 这篇文章主要介绍了Html5 new XMLHttpRequest()监听附件上传进度,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文主要介绍new XMLHttpRequest()监听附件上传进度,解决优化loading长时间加载,用户等待问题
一、存在问题
经测试发现,new XMLHttpRequest()在附件上传请求中,WIFI关闭切4G上传,上传进度不会持续;4G不关闭打开WIFI会继续上传,但等待时间过长,实际上是4G在上传,倘若关闭4G网络,上传进度终止。
二、相关代码
2.1 HTML
提交中...0%
2.2 CSS样式
/* 附件上传进度条 */ .process-wrapper{ -moz-user-select:none; position: fixed; left: 0; top: 0; bottom: 0; right: 0; z-index: 10000; display: none; } .process-face{ width: 100%; height: 100%; background-color: #000; opacity: 0.7; position: fixed; } .close-icon{ width: 26px; height: 26px; position: fixed; left: 50%; top: calc( 50% + 40px ); transform: translate(-50%,-50%); } .process{ width: 90%; height: 30px; background-color: #fff; border-radius: 30px; overflow: hidden; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); text-align: center; font-size: 14px; line-height: 30px; color: #999; } .process-inner{ width: 100%; height: 30px; position: absolute; left: 0; top: 0; background-color: #0079C1; transition: 0.1s; z-index: -1; } 2.3 JS
(function(app, doc) { var $processWrap = document.getElementById("processWrap"), $closeBtn = document.getElementById("closeBtn"), xhr = new XMLHttpRequest(); doc.addEventListener('netchange', onNetChange, false); function onNetChange() { if ($processWrap.style.display != "none") { $processWrap.style.display = "none"; xhr.abort(); mui.toast('网络中断请重试'); } } doSend: function() { app.ajaxFile({ //封装好的ajax请求 url: "", data: FormData, xhr: xhr, success: function(r) { if (r == '1') { mui.toast("保存成功"); // 上传成功逻辑处理 } else { $processWrap.style.display = "none"; mui.toast(app.netError); } }, error: function(r) { $processWrap.style.display = "none"; }, progress: function(e) { if (e.lengthComputable) { var progressBar = parseInt((e.loaded / e.total) * 100); if (progressBar < 100) { $progress.innerHTML = progressBar + "%"; $processInner.style.width = progressBar + "%"; } } }, timeout:function(){ $processWrap.style.display = "none"; } }); }) mui.plusReady(function() { $closeBtn.addEventListener("tap",function(){ setTimeout(function(){ $processWrap.style.display = "none"; xhr.abort(); }, 400); }) }); })(app, document); 三、app.js封装ajax请求
var $ajaxCount = 0; window.app = { //ajaxFile超时时间 fileTimeout: 180000, ajaxFile: function(option) { $ajaxCount++; var _ajaxCount = $ajaxCount; if (!option.error) { option.error = ajaxError; // 请求失败提示 } if (option.validateUserInfo == undefined) option.validateUserInfo = true; var xhr = option.xhr || new XMLHttpRequest(); xhr.timeout = app.fileTimeout; xhr.open('POST', app.getItem(app.localKey.url) + option.url, true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { var r = xhr.responseText; if (r) { r = JSON.parse(r); } if (_ajaxCount == $ajaxCount) { option.success && option.success(r); } } } xhr.upload.onprogress = function (e) { option.progress(e); } xhr.onerror = function(e) { option.error(e); // 添加 上传失败后的回调函数 } xhr.ontimeout = function(e){ option.timeout(e); app.closeWaiting(); $.toast("请求超时,请重试"); xhr.abort(); } xhr.send(option.data); }, } 拓展:后端NodeJS实现代码
const express = require("express"); const multer = require("multer"); const expressStatic = require("express-static"); const fs = require("fs"); let server = express(); let upload = multer({ dest: __dirname+'/uploads/' }) // 处理提交文件的post请求 server.post('/upload_file', upload.single('file'), function (req, res, next) { console.log("file信息", req.file); fs.rename(req.file.path, req.file.path+"."+req.file.mimetype.split("/").pop(), ()=>{ res.send({status: 1000}) }) }) // 处理静态目录 server.use(expressStatic(__dirname+"/www")) // 监听服务 server.listen(8080, function(){ console.log("请使用浏览器访问 http://localhost:8080/") }); 到此这篇关于Html5 new XMLHttpRequest()监听附件上传进度的文章就介绍到这了,更多相关Html5 监听附件上传内容请搜索以前的文章或继续浏览下面的相关文章,希望大家以后多多支持!
相关内容
- HTML5 canvas实现的静态循环滚动播放弹幕基于Canvas+Vue的弹幕组件的实现前端实现弹幕效果的方法总结(包含css3和canvas的实现方式)html5使用canvas实现弹幕功能示例HTML使用canvas实现弹幕功能
- Html5+CSS3+EL表达式问题小结html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
- 天天飞车截图操作方法 天天飞车高分截图操作教程_手机游戏_游戏攻略_
- 天天飞车任务大全 天天飞车任务详细汇总_手机游戏_游戏攻略_
- 天天飞车飞车夺宝玩法及奖励一览_手机游戏_游戏攻略_
- 天天飞车车手大全 车手技能图文解析_手机游戏_游戏攻略_
- 100地牢 第31关 图文攻略_手机游戏_游戏攻略_
- 100地牢 第32关 图文攻略_手机游戏_游戏攻略_
- 100地牢 第33关 图文攻略_手机游戏_游戏攻略_
- 100地牢 第34关 图文攻略_手机游戏_游戏攻略_

