jQuery-form实现文件分步上传
分步上传:当你需要提交两个及以上的文件,在一个文件成功后再提交另一个文件,并且最后需要提交所有文件的地址组成的数据
HTML:
<form id="uploadVideoForm" enctype="multipart/form-data">
<label for="group-name">组别名称:</label>
<input type="text" name="group-name"/>
<label for="head-name">负责人:</label>
<input type="text" name="head-name"/>
<div class="check-video">
<span>视频简介:</span>
<video id="myVideo" autoPlay width="300"></video>
<input type="file" accept="video/*" class='intro-video' name="file"/>
</div>
<div class="martop">
<div class="group-intros">
<p>负责人简介</p>
<input type="text" name="group-head"/>
<p>组别介绍</p>
<input type="text" name="group-intro"/>
<p>组内成员</p>
<input type="text" name="group-people"/>
</div>
</div>
<button class="save">添加</button>
<button class="update_data">确定修改</button>
<span class="cancle">清空</span>
</form>
<form id="uploadImgForm" enctype="multipart/form-data">
<label for="video-name">负责人照片:</label>
<div id="box">
<img id="imgshow" src="" alt=""/>
</div>
<div id="pox">
<input id="filed" type="file" accept="image/*" name="file"/>
</div>
<button class="update_group"></button>
</form>
JS:在点击提交后,执行文件的提交并阻止默认提交,提交成功后或者第一个文件为空时,使用$(element).trigger('click')触发另一个表单的点击(提交)事件并阻止默认提交,成功后使用Ajax发送结果的json数据。
//新增组别ajax
newGroupAjax:function(data) {
let _this = this;
console.log(data)
let optionType = localStorage.getItem("optionType")
if(optionType === 'save') {
optionType = "/admin/add_group"
}
else {
optionType = "/admin/update_group"
data.groupId = localStorage.getItem("groupId")
} console.log(optionType)
$.ajax({
url: BASEPATH + optionType,
type: "post",
data: data,
dataType: "json",//返回数据格式为json
xhrFields:{
withCredentials:true
},
success: function(data) {
console.log(data)
if(data.status === 0) {
_this.enptyFunction()
_this.getAllGroup()
}
},
error: function(err) {
console.log(err)
}
})
},
//新增组别
createGroup:function() {
let _this = this;
let postdatas = null;
function imgFileUpload(imgValue, postdata) {
if(imgValue !== "") {
let imgfileType = _this.getFileType(imgValue)
if(imgfileType !== 'bmp' && imgfileType !== 'png' && imgfileType !== 'jpg' && imgfileType !== 'jpeg'){
_this.layerOpen("请上传png/jpg等图片类型的文件!")
$('#filed').val("");
return;
}
var optionss = {
type: 'POST',
url: BASEPATH + "/file/upload_file",
dataType: 'json',
xhrFields:{
withCredentials:true
},
success: function(data) {
console.log(data)
if(data.status === 0) {
$('#filed').val("");
localStorage.setItem('groupMainImg',data.data)
postdatas.groupMainImg = data.data _this.newGroupAjax(postdatas)
}
else if(data.status === 20002) {
_this.layerOpen(data.msg);
window.open('../index.html','_self')
}else {
_this.layerOpen(data.msg);
$('#filed').val("");
}
},
error : function(xhr, status, err) {
_this.layerOpen("操作失败");
}
};
$("#uploadImgForm").submit(function(e){
$(this).ajaxSubmit(optionss);
return false; //防止表单自动提交
});
}else {
postdatas.groupMainImg = ""
$("#uploadVideoForm").submit(function(e){
return false;
});
_this.newGroupAjax(postdatas)
}
}
//视频图片
$('body').on('click', '.update_group', function(e) {
if(e.target.className === 'update_group' && flag1) {
let times = 60
flag1 = false;
let timers = setInterval(function() {
times --;
if(times <= 0) {
clearInterval(timers);
flag1 = true;
return;
}
},1000)
let imgValue = $('#filed').val()
imgFileUpload(imgValue)
}
})
//新增/修改
$('body').on('click', '.save,.update_data', function(e) {
let targetName = e.target.className
localStorage.setItem("optionType",targetName)
let group_name = $('input[name="group-name"]').val()//组别名称
let head_name = $('input[name="head-name"]').val()//负责人
// let intro_video = $(".intro-video").val($(inputs[4]).val())//简介视频
// let head_filed = $("#filed").val($(inputs[2]).val())//负责人头像
let group_head = $("input[name='group-head']").val()//负责人简介
let group_intro = $("input[name='group-intro']").val()//组别介绍
let group_people = $("input[name='group-people']").val()//组别成员
if(group_name.trim() === '' || UNAMERE.test(group_name)){
_this.layerOpen('组别名称不能为空或含特殊字符!');
return;
}
if(head_name.trim() === '' || UNAMERE.test(head_name)){
_this.layerOpen('负责人不能为空或含特殊字符!');
return;
}
if(group_head.trim() === '' || group_head.trim() === '' || group_people.trim() === ''){
_this.layerOpen('负责人简介/组别介绍/组内成员不能为空!');
return;
}
postdatas = {
groupName: group_name,
groupMainIntroduce: group_head,
groupIntroduce: group_intro,
groupMates: group_people,
groupMainPeople: head_name
}
if(targetName === 'save' || targetName === 'update_data' && flag) {
let time = 60
flag = false;
let timer = setInterval(function() {
time --;
if(time <= 0) {
clearInterval(timer);
flag = true;
return;
}
},1000)
let introValue = $('.intro-video').val()
if(introValue !== "") {//介绍视频不为空
let introfileType = _this.getFileType(introValue)
if(introfileType !== 'mp4' && introfileType !== 'rmvb' && introfileType !== 'avi' && introfileType !== 'ts'){
_this.layerOpen("请上传mp4/rmvb等视频类型的文件!")
$('.intro-video').val("");
return;
}
var options = {
type: 'POST',
url: BASEPATH + "/file/upload_file",
dataType: 'json',
xhrFields:{
withCredentials:true
},
success: function(data) {
console.log(data)
if(data.status === 0) {
localStorage.setItem('groupVideo',data.data)
postdatas.groupVideo = data.data//postdatas赋值 $('.intro-video').val("");
$('.update_group').trigger('click')
}
else if(data.status === 20002) {
_this.layerOpen(data.msg);
window.open('../index.html','_self')
}else {
_this.layerOpen(data.msg);
$('.intro-video').val("");
}
},
error : function(xhr, status, err) {
_this.layerOpen("操作失败");
}
};
$("#uploadVideoForm").submit(function(e){
$(this).ajaxSubmit(options);
return false; //防止表单自动提交
});
}else {
postdatas.groupVideo = ""
$("#uploadVideoForm").submit(function(e){
return false;
});
$('.update_group').trigger('click')
}
}else {
_this.layerOpen("操作过于频繁!")
}
})
}
附:
jQuery-form,获取地址:链接: https://pan.baidu.com/s/10mlT36j5yizgO9Xq8OxelQ 提取码: 2jgu
发送文件示例:
注:提交的按钮需卸载form标签中,并且为button按钮
var options = {
type: 'POST',
url: BASEPATH + "/student/upload_students",
dataType: 'json',
xhrFields:{
withCredentials:true
},
success: function(data) {
if(data.status === 20000) {
_this.layerOpen("上传成功!");
$('input[name="file"]').val("");
}
else if(data.status === 20002) {
_this.layerOpen(data.msg);
window.open('../index.html','_self')
}else {
_this.layerOpen(data.msg);
$('input[name="file"]').val("");
}
},
error : function(xhr, status, err) {
_this.layerOpen("操作失败");
}
};
$("#uploadForm").submit(function(e){ //uploadForm表单名称
e.preventDefault()
$(this).ajaxSubmit(options);
return false; //防止表单自动提交
});
jQuery-form实现文件分步上传的更多相关文章
- 使用Spring和JQuery实现视频文件的上传和播放
Spring MVC可以很方便用户进行WEB应用的开发,实现Model.View和Controller的分离,再结合Spring boot可以很方便.轻量级部署WEB应用,这里为大家介绍如何使用Spr ...
- jquery.form.js ajax提交上传文件
项目中最近有用到表单提交,是带有图片上传的表单录入,需要ajax异步提交,网上找了好多例子都是只能提交上传字段一个信息的,这里整理一下.表单里有普通文本信息字段也有图片上传字段. 1.jsp代码--引 ...
- jquery.form.js实现异步上传
前台页面 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...
- [Asp.net mvc]jquery.form.js无刷新上传
写在前面 最近在自己的网盘项目中想用ajax.beginform的方式做无刷新的操作,提交表单什么的都可以,但针对文件上传,就是个鸡肋.在网上查找了发现很多人都遇到了这个问题,大部分都推荐使用jque ...
- jquery.form.js 实现异步上传
前台: <form id="formSeacrh" action="/ResumeInfo/uploadFile" method="post&q ...
- asp.net使用jquery.form实现图片异步上传
首先我们需要做准备工作: jquery下载:http://files.cnblogs.com/tianguook/jquery1.8.rar jquery.form.js下载:http://files ...
- jQuery.form Ajax无刷新上传错误 (jQuery.handleError is not a function) 解决方案
今天,随着ajaxfileupload时间firebug财报显示,"jQuery.handleError is not a function"错误.因为一旦使用jQuery.for ...
- 解决 jquery.form.js和springMVC上传 MultipartFile取不到信息
前段页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- 文件上传---form表单,ajax,jquery,以及iframe无刷新上传 (processData,contentType讲解)
服务端程序: import tornado.web import os IMG_LIST=[] class IndexHandler(tornado.web.RequestHandler): def ...
随机推荐
- Java多线程:CountDownLatch、CyclicBarrier 和 Semaphore
场景描述: 多线程设计过程中,经常会遇到需要等待其它线程结束以后再做其他事情的情况. 有几种方案: 1.在主线程中设置一自定义全局计数标志,在工作线程完成时,计数减1.主线程侦测该标志是否为0,一 ...
- Android中使用BufferedReader.readline阻塞读取不到数据,但是ready返回true
通过socket测试工具在电脑上发送消息,Android真机可以收到响应BufferedReader.ready()返回true,但是readline却一直阻塞. 原因:readline()只有在遇到 ...
- java使用代理发post请求
这东西啊,本身是无用的,但是要是移植就有用. package util; import java.util.Properties; public class HttpProxyConfiger { p ...
- 入围T00ls 2018风云人物
今天早上打开T00ls,发现成功入围了<T00ls第六届(2018)年度人物风云榜>,共34名年度人物,每个id可投10票,34选10. T00ls是当前国内为数不多的民间网络信息安全研究 ...
- VS F5不编译 F5总是重新编译
遇到奇怪的现象,F5不编译了 右键解决方案-配置管理器-确保项目的生成被勾选 另外一个情况,即使不修改任何代码,每次点击“生成”或者F5,都会重新编译(Debug模式没问题,Release有这个问题, ...
- 从unity丢图标到unity进不去桌面
现象1: 用了一年多的unity的右上角的网络图标和网易云音乐的图标消失不见了,我也不记得最近有update或upgrade过,然而这两个功能还是可以正常用 解决1: 安装Gnome,果然相应的图标就 ...
- pandas replace 替换功能function
list like replace method dict like replace method regex expression import pandas as pd import numpy ...
- react 使用 lazyload 懒加载图片
文档地址 index.html <script> (function(w, d) { var b = d.getElementsByTagName("body")[0] ...
- js中的异步与同步,解决由异步引起的问题
之前在项目中遇到过好多次因为异步引起的变量没有值,所以意识到了认识js中同步与异步机制的重要性 在单线程的js中,异步代码会被放入一个事件队列,等到所有其他代码执行后再执行,而不会阻塞线程. 下面是j ...
- phpmyadmin新姿势getshell
旁白:在一个有WAF.并且mysql中的Into outfile禁用的情况下,我该如何getshell? 作者:倾旋email:payloads@aliyun.com投稿联系:service@cora ...