日期:2014-05-17  浏览次数:20440 次

前台JS调用后台 函数问题
前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DownLoadFile._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title></title>
  <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <input type="submit" value="提交" id="OK" />
  <input visible="false" id="Hidden" />
  </div>
  </form>
  <script>
  $("#id").click()
  {
  document.write("<%ExportToExcel(); %>");
  }
  </script>
</body>
</html>

后台代码

public void ExportToExcel()
  {
  string strFile = " ";
  string path = " ";
  strFile = strFile + "export_ ";
  strFile = strFile + DateTime.Now.ToString("yyyyMMddHHmm ");
  strFile = strFile + ".xls ";
  string fileName = strFile;
  path = Server.MapPath("/") + "DownLoad\\" + fileName;
  System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
  StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());

  /*DataRow[] myRow = dt.Select("");

  //取得数据表各列标题,标题之间以\t分割,最后一个列标题后加回车符
  for (int i = 0; i < dt.Columns.Count; i++)
  {
  sw.Write(dt.Columns[i].Caption.ToString() + "\t");
  }
  sw.Write("\n");
  //逐行处理数据
  foreach (DataRow row in myRow)
  {
  //在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n
  for (int i = 0; i < dt.Columns.Count; i++)
  {
  sw.Write(row[i].ToString() + "\t");
  }
  sw.Write("\n");
  }*/

  sw.Flush();
  sw.Close();
  if (path != null)
  {
  //DownFile(path,fileName);
  try
  {
  DownFile(path, fileName);
  //DownFile(path, fileName);
  }
  catch (Exception e)
  {
  Response.Write("<script>alert('系统出现以下错误:\\n" + e.Message + "!\\n请尽快与管理员联系.')</script>");
  }
  }
  }

  private void DownFile(string path, string fileName)
  {
  //string fileName = "asd.txt";//客户端保存的文件名 

  //string filePath = Server.MapPath(path);//路径

  FileInfo fileInfo = new FileInfo(path);

  Response.Clear();

  Res