mdn

前端代码 github

<body class="m-2">
<label for="a" class="btn btn-primary">点击上传</label>
<input id='a' name="file" type="file" accept="image/png, image/jpeg, video/*" style="display:none;" multiple='multiple'>
<script>
async function main() { const l = console.log
let fileEle = document.querySelector('#a')
fileEle.onchange = e => {
let files = fileEle.files
if(files.length === 0) return false; let data = new FormData()
for(const file of files){
data.append('files', file)
} let xhr = new XMLHttpRequest() xhr.upload.addEventListener('progress', e => {
if (e.lengthComputable) {
var percentage = Math.round((e.loaded * 100) / e.total);
l(`${percentage}%`)
}
}) xhr.open('post', 'http://localhost:5000/upload')
xhr.responseType = 'json'
xhr.send(data) xhr.upload.addEventListener('loadstart', e => {
l('上传开始')
}) xhr.upload.addEventListener('error', e => {
l('上传失败')
}) xhr.upload.addEventListener('abort', e => {
l('上传终止')
}) xhr.upload.addEventListener('timeout', e => {
l('由于预设时间到期,上传终止')
}) xhr.upload.addEventListener('load', e => {
l('上传成功了')
}) xhr.upload.addEventListener('loadend', e => {
l('上传已经停止了')
}) xhr.onload = () => {
l(...xhr.response.imgsSrc);
} }
}
main();
</script>
</body> </html>

后台代码片段

  @Post('upload')
@UseInterceptors(FilesInterceptor('files'))
uploadfile(@UploadedFiles() files, @Body() body) { if (!files || files.length === 0) {
throw new HttpException('参数错误', HttpStatus.FORBIDDEN)
} let imagesSrc = []
for (const file of files) {
const imgName = `${Date.now()}-${file.originalname}`
const writeImage = createWriteStream(join(__dirname, '..', 'upload', imgName))
writeImage.write(file.buffer)
imagesSrc.push( `http://localhost:5000/images/${imgName}` )
}
return {
imgsSrc: imagesSrc
}
}

ajax 上传文件,监听进度(progress)的更多相关文章

  1. ajax 上传文件,显示进度条,进度条100%,进度条隐藏,出现卡顿就隐藏进度条,显示正在加载,再显示上传完成

    <form id="uploadForm" method="post" enctype="multipart/form-data"&g ...

  2. Ajax上传文件进度条显示

    要实现进度条的显示,就要知道两个参数,上传的大小和总文件的大小 html5提供了一个上传过程事件,在上传过程中不断触发,然后用已上传的大 小/总大小,计算上传的百分比,然后用这个百分比控制div框的显 ...

  3. ajax上传文件显示进度

    下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...

  4. ajax上传文件及进度显示

    之前在博文:原生ajax写法就提及过ajax2.0与1.0的差别是多了FormData和利用FormData文件上传(当然还有跨域,但不是本文的重点). 那么具体怎么样实现ajax上传文件呢? 一般来 ...

  5. HTML5 jQuery+FormData 异步上传文件,带进度条

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link href ...

  6. atitit.ajax上传文件的实现原理 与设计

    atitit.ajax上传文件的实现原理 与设计 1. 上传文件的三大难题 1 1.1. 本地预览 1 1.2. 无刷新 1 1.3. 进度显示 1 2.  传统的html4  + ajax 是无法直 ...

  7. Spring Boot上传文件(带进度条)

    Spring Boot 上传文件(带进度条)# 配置文件 spring: freemarker: template-loader-path: classpath:/static/ ##Spring B ...

  8. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  9. springMVC+jsp+ajax上传文件

    工作中遇到的小问题,做个笔记 实现springMVC + jsp + ajax 上传文件 HTML <body> <form id="myform" method ...

  10. 伪ajax上传文件

    伪ajax上传文件   最近在折腾伪ajax异步上传文件. 网上搜索了一下,发现大部分方法的input file控件都局限于form中,如果是在form外的呢? 必须动态生成一个临时form和临时if ...

随机推荐

  1. Python常用的软件包

    下面是Python开发常用的软件包. 名称 用途 安装命令 opengl   sudo pip3 install PyOpenGL  pyqtgraph GUI图形库 sudo pip3 instal ...

  2. centos下安装wireshark 抓包

    centos下安装wireshark相当简单.两条命令就够了.这里.主要是记录写使用方面的东西 安装:1.yum install wireshark.注意这样并无法使用wireshark命令和图形界面 ...

  3. Lua游戏开发之时区问题

    目前大部分游戏都采用了Lua语言进行功能开发,在进行多语种发行的时候就会遇到时区显示的问题.以韩国版本为例,场景如下: 1.服务器处于固定的位置,比如放在首尔机房: 2.玩家所处的位置不确定,可能在韩 ...

  4. 怎样让两个DIV在同一水平线上面显示

    css定义第二个div. float:right或者left. margin-top:0px 确保第二个DIV的宽度.如果宽度宽的话,会自动到下方的.

  5. Webdings字体和Wingdings字体对照表

    一.Webdings是一个TrueType的dingbat字体,于1997年发表,搭载在其后的Microsoft Windows视窗系统内,大多的字形都没有Unicode的相对字. 使用很简单1.网页 ...

  6. PHP中一些常用知识点

    1.json字符串转json对象 $data='[{"user_id":"93","price":"52.50"},{& ...

  7. Redis数据结构详解,五种数据结构分分钟掌握

    redis数据类型分为:字符串类型.散列类型.列表类型.集合类型.有序集合类型.redis这么火,它运行有多块?一台普通的笔记本电脑,可以在1秒钟内完成十万次的读写操作.原子操作:最小的操作单位,不能 ...

  8. Nginx防压力测试

    一.ab压力测试方式为: $ab -n 1000 -c 100 http://www.abc.com:80/ 二.直接简单的方法限制同一个IP的并发最大为10:(以宝塔管理工具为例) 1.打开Ngin ...

  9. js常用加解密函数汇总

    1. JS自定义加密解密函数,及用法 function compile(code) { )+code.length); ;i<code.length;i++){ c+=String.fromCh ...

  10. 为什么要使用NoSQL

    转载自:http://www.infoq.com/cn/news/2011/01/nosql-why [编者按]NoSQL在2010年风生水起,大大小小的Web站点在追求高性能高可靠性方面,不由自主都 ...