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

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

公司主营业务:成都网站建设、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出崇信免费做网站回馈大家。

引入脚本:


//前端代码:
󰀠 浏览文件 下载范例
//提交表单 $("#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();

/// 
 /// Excel文件导成Datatable
 /// 
 /// Excel文件目录地址
 /// Datatable表名
 /// Excel sheet index
 /// 
 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;
 }

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


文章标题:jquery.form.js异步提交表单详解
标题路径:http://myzitong.com/article/jddoph.html