jquery.form.js异步提交表单详解

发布时间:2020-09-25 17:04:02 作者:阳光秋天
来源:脚本之家 阅读:157

本文实例为大家分享了jquery.form.js异步提交表单的具体代码,供大家参考,具体内容如下

引入脚本:

<script type="text/javascript" src="/js/jquery.form.min.js"></script>
<script src="/js/json2.js"></script>
//前端代码:
<form id="f1">
  <span class="btn-upload form-group">
   <input class="input-text upload-url radius" type="text" name="uploadfile-1" id="uploadfile-1" readonly><a href="javascript:void();" rel="external nofollow" class="btn btn-primary radius"><i class="iconfont">&#xf0020;</i> 浏览文件</a>
   <input type="file" multiple name="file-1" class="input-file">
  </span>
  <input type="button" class="btn btn-warning radius mt-10 f-l mr-10" onclick="ImportData(this)" value="保存"/>
  <a href="/files/企业表.xls" rel="external nofollow" target="_blank" class="f-16 l-30 mt-10 f-l c-danger">下载范例</a>
  <div class="clearfix"></div>
 </form>


//提交表单
  $("#f1").ajaxSubmit({
   url: "/ImportAndCheck/ImportCompanyOperation/",
   type: "post",
   enctype: "multipart/form-data",
   dataType: "html",
   beforeSubmit: function () {
    $(obj).val("保存中...").attr("disabled", true);
   },
   success: function (response) {
    try {
     data = JSON.parse(response);
     if (data.code > 0) {
      layer.msg(data.msg, { time: 1000 });
      setTimeout(function () {
       window.location.href = "/ImportAndCheck/ImportCompany?status=0";
      }, 1000);
     } else {
      layer.msg(data.msg, { time: 1000 });
      $(obj).val("保存").attr("disabled", false);
     }
    } catch (err) {
     layer.msg("导入失败!", { time: 1000 });
     $(obj).val("保存").attr("disabled", false);
    }

   },
   error: function (XmlHttpRequest, textStatus, errorThrown) {
    layer.msg("导入失败", { time: 1000 });
    $(obj).val("保存").attr("disabled", false);
   }
  });

//服务端
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
 ReturnJsonCode jsonCode = new ReturnJsonCode();
   if (files == null || files.Count == 0)
   {
    jsonCode.msg = "请选择上传的导入文件";
    return jsonCode;
   }
   //验证文件类型
   if (files != null && files.Count > 0)
   {
    HttpPostedFile file = null;
    file = files[0];
    //扩展名检查
    string picext = System.IO.Path.GetExtension(file.FileName).ToLower();
    if (picext != ".xls")
    {
     jsonCode.msg = "上传文件仅支持xls文档格式";
     return jsonCode;
    }

    DataTable db = ExcelToDataTable(file, file.FileName, 0);
    int failcount = 0;
    int successcount = 0;
    if (db != null && db.Rows.Count > 0)
    {
     foreach (DataRow dr in db.Rows)
     {
      string mobile = dr["手机号码"].ToString().Trim();
      string truename = dr["姓名"].ToString().Trim();
      string iskejizhuanyuan = dr["科技专员"].ToString().Trim();
      string sex = dr["性别"].ToString().Trim();

/// <summary>
 /// Excel文件导成Datatable
 /// </summary>
 /// <param name="strFilePath">Excel文件目录地址</param>
 /// <param name="strTableName">Datatable表名</param>
 /// <param name="iSheetIndex">Excel sheet index</param>
 /// <returns></returns>
 public static DataTable ExcelToDataTable(string strFilePath, string strTableName, int iSheetIndex)
 {
  var strExtName = Path.GetExtension(strFilePath);

  var dt = new DataTable();
  if (!string.IsNullOrEmpty(strTableName))
  {
  dt.TableName = strTableName;
  }

  if (strExtName.Equals(".xls"))
  {
  using (var file = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
  {
   var workbook = new HSSFWorkbook(file);
   var sheet = workbook.GetSheetAt(iSheetIndex);

   //列头
   foreach (var item in sheet.GetRow(sheet.FirstRowNum).Cells)
   {
   dt.Columns.Add(item.ToString(), typeof(string));
   }

   //写入内容
   var rows = sheet.GetRowEnumerator();
   while (rows.MoveNext())
   {
   IRow row = (HSSFRow)rows.Current;
   if (row.RowNum == sheet.FirstRowNum)
   {
    continue;
   }

   var dr = dt.NewRow();
   foreach (var item in row.Cells)
   {
    switch (item.CellType)
    {
    case CellType.Boolean:
     dr[item.ColumnIndex] = item.BooleanCellValue;
     break;
    case CellType.Error:
     dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
     break;
    case CellType.Formula:
     switch (item.CachedFormulaResultType)
     {
     case CellType.Boolean:
      dr[item.ColumnIndex] = item.BooleanCellValue;
      break;
     case CellType.Error:
      dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
      break;
     case CellType.Numeric:
      if (DateUtil.IsCellDateFormatted(item))
      {
      dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
      }
      else
      {
      dr[item.ColumnIndex] = item.NumericCellValue;
      }
      break;
     case CellType.String:
      var str = item.StringCellValue;
      if (!string.IsNullOrEmpty(str))
      {
      dr[item.ColumnIndex] = str;
      }
      else
      {
      dr[item.ColumnIndex] = null;
      }
      break;
     case CellType.Unknown:
     case CellType.Blank:
     default:
      dr[item.ColumnIndex] = string.Empty;
      break;
     }
     break;
    case CellType.Numeric:
     if (DateUtil.IsCellDateFormatted(item))
     {
     dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
     }
     else
     {
     dr[item.ColumnIndex] = item.NumericCellValue;
     }
     break;
    case CellType.String:
     var strValue = item.StringCellValue;
     if (!string.IsNullOrEmpty(strValue))
     {
     dr[item.ColumnIndex] = strValue;
     }
     else
     {
     dr[item.ColumnIndex] = null;
     }
     break;
    case CellType.Unknown:
    case CellType.Blank:
    default:
     dr[item.ColumnIndex] = string.Empty;
     break;
    }
   }
   dt.Rows.Add(dr);
   }
  }
  }
  return dt;
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Jquery 异步提交表单(post)
  2. jquery提交表单jquery.form.js

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jquery.form.js 表单 fo

上一篇:ASP.NET MVC4+BootStrap 实战(三)

下一篇:Vestacp整合WHMCS实现自动销售开通虚拟主机服务教程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》