使用jquery.form.js实现文件上传及进度条前端代码
来源:互联网 发布:网页数据采集器 编辑:程序博客网 时间:2024/06/06 03:45
1、背景
ajax的表单提交只能提交data数据到后台,没法实现file文件的上传还有展示进度功能,这里用到form.js的插件来实现,搭配css样式简单易上手,而且高大上,推荐使用。
2、静态页搭建
html代码如下
<div class="upload-fileWrap"> <button type="button" id="upload-input-btn" class="lx-btn lx-btn-default-fl">选择文件</button> <form id='myupload' name='myupload' action='' method='post' enctype='multipart/form-data'> <input id="upload-input-file" class="upload-input-file" name="file" type="file" accept="audio/mpeg, audio/x-wav" data-value-update="input"> </form> <div class="upload-file-stateWrap"> <span class="upload-file-result"></span> <div class="progress hidden"> <div class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"> <span class="progress-bar-status">0%</span> </div> </div> </div></div>
需要解释下我的结构,#upload-input-file
的input标签是真实的文件上传按钮,包裹form标签后可以实现上传功能,#upload-input-btn
的button标签是展示给用户的按钮,因为需要样式的美化。上传完成生成的文件名将会显示在.upload-file-result
里面,.progress
是进度条的位置,先让他隐藏加上hidden
的class,.progress-bar
是进度条的主体,.progress-bar-status
是进度条的文本提醒。
下面添加需要的css
.hidden{display:none;}.upload-fileWrap { margin: 3px 0 0 -2px; position: relative;}.upload-input-file { position: absolute; left: 2px; top: 0; display: inline-block; width: 88px; height: 34px; line-height: 34px; opacity: 0; cursor: pointer; z-index: 2;}.upload-file-result { color: #a1acc6; font-size: 14px;}/*进度条*/.progressWrap { position: absolute; right: 20px; top: 56px; width: 200px; height: 10px;}.progress { width: 100%; height: 10px; background: #0f1529; -webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px; overflow: hidden;}.progress-bar { height: 10px; background: url("../img/progress-line.png") repeat-x;}.progress-bar span { position: absolute; color: #58637e; font-size: 12px; line-height: 18px;}.progress-bar span.progress-bar-status { left: 50%; top: -23px; margin-left: -15px; color: #1cc3b0;}.upload-file-stateWrap { position: relative; width: 100%; height: auto;}.upload-file-stateWrap .progress { width: 60%;}.upload-file-stateWrap span.progress-bar-status { top: inherit; bottom: -3px; left: 60%; margin-left: 5px;}
去掉hidden的class,看到的效果是这样的
3、上传文件脚本
将上传事件绑定在file的input里面,绑定方式就随意了。
var progress = $(".progress-bar"),
status = $(".progress-bar-status"),
percentVal = '0%';
//上传步骤
$("#myupload").ajaxSubmit({
url: uploadUrl,
type: "POST",
dataType: 'json',
beforeSend: function () {
$(".progress").removeClass("hidden");
progress.width(percentVal);
status.html(percentVal);
},
uploadProgress: function (event, position, total, percentComplete) {
percentVal = percentComplete + '%';
progress.width(percentVal);
status.html(percentVal);
console.log(percentVal, position, total);
},
success: function (result) {
percentVal = '100%';
progress.width(percentVal);
status.html(percentVal);
//获取上传文件信息
uploadFileResult.push(result);
// console.log(uploadFileResult);
$(".upload-file-result").html(result.name);
$("#upload-input-file").val('');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown);
$(".upload-file-result").empty();
}
});
- ajaxSubmit 插件封装的提交方法;
- beforeSend 提交前的回调函数;
- uploadProgress 提交中的回调函数,position, total, percentComplete是三个返回值,position是已经上传完成的字节数,total表示总的字节数,percentComplete表示已完成的比例,打印出来如下图,就是利用这里返回的数据制作进度条的
- success 表示上传成功的回调函数。
上传中和上传完成的截图如下:
更多用法可以参考官网
- 使用jquery.form.js实现文件上传及进度条前端代码
- 【jQuery】Jquery.form.js实现表单异步提交以及文件上传(带进度条)
- 简单实现:jquery ajax/jquery.form.js + springmvc上传文件,带进度条
- 使用jquery.form.js+servlet实现文件异步上传
- 使用jquery.form.js实现无刷新上传文件
- jQuery实现jQuery-form.js实现异步上传文件
- struts2实现文件上传进度条(前端JS+Java)(收藏)
- 实现jQuery-form.js实现异步上传文件
- jquery ajax实现上传文件代码,带进度条
- jquery ajax实现上传文件代码,带进度条
- jquery.form实现文件上传
- jfinal+jsp+jquery.form.js+oracle实现上传文件
- 使用jquery.form.js上传图片或文件
- 使用jquery-form.js异步上传文件和提交表单
- 关于使用jQuery-form.js上传文件的注意事项
- jquery form插件(ajax)上传文件实现及原理
- jquery的异步提交表单(异步上传文件)及jquery.form.js上传文件注意事项
- 使用jQuery.form插件实现表单异步提交+上传文件
- 关于django设置表存储引擎
- Spring MVC之@RequestBody, @ResponseBody 详解
- 行人检测(二)
- Linux下Graphics2D.drawString(message, px, py)乱码问题
- C++中##(两个井号)和#(一个井号)用法
- 使用jquery.form.js实现文件上传及进度条前端代码
- 微信小程序之购物车
- new、abstract、virtual、override,sealed关键字区别和使用代码示例
- linux C之access函数
- map--已知key取对应value
- 图像分割“Not All Pixels Are Equal: Difficulty-Aware Semantic Segmentation via Deep Layer Cascade”
- Java中堆和栈的分析
- 学习【OpenCV入门教程之十四】OpenCV霍夫变换:霍夫线变换,霍夫圆变换合辑---笔记
- 谷歌微软等科技巨头数据科学面试107道真题:你能答出多少?