$("form").serialize()和 new FormData($('#uploadForm')[0])都是序列化表单,实现表单的异步提交,但是二者有区别

首先,前者,只能序列化表单中的数据 ,比如文本框等input  select等的数据,但是对于文件,比如文件上传,无法实现,那么这时候,FormData就上场了,

new FormData使用需要有一个注意点,

注意点一:,对于jquery的要求是,好像是 版本1.8及其以上方可支持。

另外该对象不仅仅可以序列化文件,一样可以用作表单数据的序列化,(就是说包含了serialize()的功能);

注意点二:看脚本

 $.ajax({
type: 'POST',
data: uploadFormData,
url: '/Artical/Publist',//TypeError: 'append' called on an object that does not implement interface FormData.
processData: false,
contentType: false,
async: false,
success: function (data) {
if (typeof (data) == undefined) {
alert("用户信息已丢失,请重新登录!"); window.parent().location.href = "/Account/Login";
}
if (data.ErrorMsg == "") {
alert('美文发布成功!');
} else { alert(data.ErrorMsg); }
}
});

  注意红色部分脚本以及说明,

processData: false, contentType: false,缺少这二者的设置,将会出现  红色部分的错误提示,提交失败。

以下是一个完整的前后台的参考脚本:
//提交文件
function submitFile() {
$('.btn-publish').click(function () {
//var title = $('.txt-video-title').val();
var uploadFormData = new FormData($('#uploadForm')[]);//序列化表单,$("form").serialize()只能序列化数据,不能序列化文件
$.ajax({
type: 'POST',
data: uploadFormData,
url: '/Artical/Publist',//TypeError: 'append' called on an object that does not implement interface FormData.
processData: false,
contentType: false,
async: false,
success: function (data) {
if (typeof (data) == undefined) {
alert("用户信息已丢失,请重新登录!"); window.parent().location.href = "/Account/Login";
}
if (data.ErrorMsg == "") {
alert('美文发布成功!');
} else { alert(data.ErrorMsg); }
}
}); });
}
/// <summary>
/// 上传新图片,(包含文件上传)
/// </summary>
/// <returns></returns>
public JsonResult UpLoad()
{
if (null != Session[Consts.SYSTEMUERSESSION])
{
string pictureName = Request["videoTitle"];//图片标题
string pictureInfoUrl = "";//图片上传之后的虚拟路径
string pictureCategoryKey = Request["PictureCategoryList"];//视频分类外键ID FileUpLoadResult fileUpLoadPicture = null;//用于输出结果 string fileSavePath = Consts.PICTURESAVEPATH + DateTime.Now.ToString("yyyyMMdd") + "/";//当天时间最为文件夹
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");//生成的文件名称
//上传,如果是视屏文件,自动生成 接切图
fileUpLoadPicture = Request.UpLoad(fileSavePath, null, "", fileName, ""); #region 装箱、入库
if (fileUpLoadPicture.FileSavePath != null)
{
foreach (var path in fileUpLoadPicture.FileSavePath)
{
pictureInfoUrl += (path + ",");
}
pictureInfoUrl = pictureInfoUrl.Remove(pictureInfoUrl.Length - , );
ColumnPicture picture = new ColumnPicture()
{
Id = CombHelper.NewComb(),
PictureTitle = pictureName,
PictureTitleDescription = pictureInfoUrl,
GoodClickTimes = ,
BadClickTimes = ,
AddDate = DateTime.Now,
FavoriteTimes = ,
IsEnabled = true,
ToTop = ,
CustomerKey = ((Customer)Session[Consts.SYSTEMUERSESSION]).Id,
ColumnsCategoryKey = new Guid(pictureCategoryKey)
};
if (_pictureService.Insert(picture))
{
fileUpLoadPicture = new FileUpLoadResult()
{
Status = true,
FileSavePath = null,
ErrorMsg = ""
};
}
}
#endregion return Json(fileUpLoadPicture, JsonRequestBehavior.AllowGet);
}
return null;
}

关于jquery的 $("form").serialize()和 new FormData表单序列化的更多相关文章

  1. (转)jquery serialize表单序列化,当radio或checkbox 未选中时,没有序列化到对象中的原因分析和解决方案 - ghostsf

    相信很多人都用过jq的表单序列化serialize()方法,因为这能很方便地帮你把表单里所有的非禁用输入控件序列化为 key/value 对象,不需要你再去一个个地拼接参数了. 这是一个很好用的函数, ...

  2. jQuery实现form表单序列化转换为json对象功能示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. jquery ajax(5)form表单序列化

    form表单序列化<script type="text/javascript"> $(function(){ $("#send").click(fu ...

  4. 原生JS实现表单序列化serialize()

    有一个form表单,要用AJAX后台提交,原来想拼接json,但是数据多了麻烦,不灵活. 用HTML5的FormData来初始化表单 var formdata=new FormData(documen ...

  5. 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。

    http://zhengxinlong.iteye.com/blog/848712 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name& ...

  6. 原生js实现form表单序列化

    当我们有form表单而且里面的表单元素较多时,咱们总不能一个个去获取表单元素内的值来进行拼接吧!这样会很让人蛋疼!为了方便与后台交互并且提高自己的开发效率,并且不让你蛋疼:我们一起用原生来写一个表单序 ...

  7. js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么

    js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么 一.总结 一句话总结:两者都是对表单进行序列化,serializeArray()返回的是json ...

  8. Jquery表单序列化和AJAX全局事件

    Jquery表单序列化 1.必须放在form标签内: 2.控件必须有name属性: 3.控件的value值会提交到服务器: 如: <form id="form1"> & ...

  9. jQuery UI 对话框(Dialog) - 模态表单

    <!doctype html><html lang="en"><head> <meta charset="utf-8" ...

随机推荐

  1. JqueryUI 为什么TypeError: $(...).slides is not a function

    单独写一个html发现一切没有问题,但放在自己的网页中作为一部分却出现了问题,最后发现是那些js文件引入顺序出现了问题,

  2. 报错java.net.SocketException: Software caused connection abort: recv failed 怎么办

    产生这个异常的原因有多种方面,单就如 Software caused 所示, 是由于程序编写的问题,而不是网络的问题引起的. 已知会导致这种异常的一个场景如下: 客户端和服务端建立tcp的短连接,每次 ...

  3. easy_install和pip区别

    easy_insall的作用和perl中的cpan, ruby中的gem类似,都提供了在线一键安装模块的傻瓜方便方式,而pip是easy_install的改进版, 提供更好的提示信息,删除packag ...

  4. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  5. decodeURIComponent

    var s = '%%' try { s = decodeURIComponent(s) } catch(e) { console.log(e) } console.log(s)

  6. -_-#flash播放器自适应

    设置断点,几个断点下的固定布局

  7. java 去掉字符串右侧空格

    public static String rightTrim(String str) {    String regex = "(.*\\S+)(\\s+$)";    Patte ...

  8. WIA设备批量扫描

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. iOS设备的重力感应

    重力感应是每台iOS设备都具备的功能,所以在应用用好重力感应会有意想不到的效果 1.添加CoreMotion框架 2.在需要使用重力感应的类中添加头文件 #import <CoreMotion/ ...

  10. Java正则表达式(1)

    String类的三个内建正则表达式工具: 1.matches()方法 示例:检查一个句子是否以大写字母开头,以句号结尾 public static boolean checkFormat(String ...