分步上传:当你需要提交两个及以上的文件,在一个文件成功后再提交另一个文件,并且最后需要提交所有文件的地址组成的数据

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实现文件分步上传的更多相关文章

  1. 使用Spring和JQuery实现视频文件的上传和播放

    Spring MVC可以很方便用户进行WEB应用的开发,实现Model.View和Controller的分离,再结合Spring boot可以很方便.轻量级部署WEB应用,这里为大家介绍如何使用Spr ...

  2. jquery.form.js ajax提交上传文件

    项目中最近有用到表单提交,是带有图片上传的表单录入,需要ajax异步提交,网上找了好多例子都是只能提交上传字段一个信息的,这里整理一下.表单里有普通文本信息字段也有图片上传字段. 1.jsp代码--引 ...

  3. jquery.form.js实现异步上传

    前台页面 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

  4. [Asp.net mvc]jquery.form.js无刷新上传

    写在前面 最近在自己的网盘项目中想用ajax.beginform的方式做无刷新的操作,提交表单什么的都可以,但针对文件上传,就是个鸡肋.在网上查找了发现很多人都遇到了这个问题,大部分都推荐使用jque ...

  5. jquery.form.js 实现异步上传

    前台: <form id="formSeacrh" action="/ResumeInfo/uploadFile" method="post&q ...

  6. asp.net使用jquery.form实现图片异步上传

    首先我们需要做准备工作: jquery下载:http://files.cnblogs.com/tianguook/jquery1.8.rar jquery.form.js下载:http://files ...

  7. jQuery.form Ajax无刷新上传错误 (jQuery.handleError is not a function) 解决方案

    今天,随着ajaxfileupload时间firebug财报显示,"jQuery.handleError is not a function"错误.因为一旦使用jQuery.for ...

  8. 解决 jquery.form.js和springMVC上传 MultipartFile取不到信息

    前段页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  9. 文件上传---form表单,ajax,jquery,以及iframe无刷新上传 (processData,contentType讲解)

    服务端程序: import tornado.web import os IMG_LIST=[] class IndexHandler(tornado.web.RequestHandler): def ...

随机推荐

  1. VM虚拟机下安装CentOS_6.5_x64

    在开始Linux的学习之前,我们必须要有一个实验环境.这里我选择了安装CentOS-6.5-x86_64的Minimal版本,大家也可以选择安装其他的Linux发行版.选择CentOS主要还是因为它比 ...

  2. windows保存的文件传输到linux中格式转换

    直接从window传输到linux的脚本执行时,会出现以下错误. -bash: xxx: /bin/sh^M: bad interpreter: No such file or directory 解 ...

  3. 如何将本地项目上传至GitHub(so easy!!!)

    如何将本地项目上传至GitHub 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直 ...

  4. linux php --ini

    $ php --ini

  5. 【网络编程】——Lighttpd 返回HTTP/1.1 417 Expectation Failed

    最近在使用python 的 pcurl 发送 post 请求到服务端的时候[服务端使用的服务是Lighttpd],发现只要 post 请求的数据超过 1024 之后,就会返回如下错误: * Hostn ...

  6. windows 安装python pip Could not install packages due to anEnvironmentError: [WinError 5] 拒绝访问

    找打 C:\Windows\System32 文件夹下面的cmd.exe点开后运行python -m pip install --upgrade pip 即解决问题了.

  7. 【转】Linux netstat命令详解,高级面试必备

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  8. 智能聊天机器人——基于RASA搭建

    前言: 最近了解了一下Rasa,阅读了一下官方文档,初步搭建了一个聊天机器人. 官方文档:https://rasa.com/docs/ 搭建的chatbot项目地址: https://github.c ...

  9. react项目,build以后启动问题

    用脚手架create-react-app创建的react项目,已经集成了webpack,只要运行命令:npm run build 项目就会编译成功,生成一个build文件夹,现在问题来了,如何启动这个 ...

  10. puppet(1)-简介

    puppet: 开源的.新一代的集中化的配置管理工具: 目标状态 配置语言 Luke Kanies,puppet labs bootstrap --> configuration --> ...