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

如何用asp.net自动下载数据库文件到本地
数据库表中有三个字段,ID, htm,和jpg都是二进制形式的,htm和jpg在一个文件夹的时候,htm里表格中会显示jpg的图片.htm和jpg都以ID为文件名.
  
  现在想让一个asp.net一页面中,输入ID,再点一个按钮的时候,可以看到html中的网页,且网页中图片正常显示.


  试过在服务器端建立相应的以ID为名的jpg和html文件,可行,但感觉这方法不好,想写在客户端,
if (File.Exists(@"D:\c#\zhiban2\yuns.htm"))
  {
  FileInfo info = new FileInfo(@"D:\c#\zhiban2\yuns.htm");
  info.Delete();
  }



  filesize = getfilesize(banh);
  SqlDataReader read = null;
  SqlCommand SqlCmd = new SqlCommand();
  SqlCmd.Connection = new SqlConnection(dbcon);
  SqlCmd.CommandText = "select top 1 htm from 文件 where ID='" + banh + "'";
  SqlCmd.CommandType = CommandType.Text;
  SqlCmd.Connection.Open();
  SqlDataAdapter SqlAd = new SqlDataAdapter(SqlCmd);
  DataSet Rs = new DataSet();
  SqlAd.Fill(Rs);
  DataTable dt = Rs.Tables[0];
  read = SqlCmd.ExecuteReader();
  read.Read();
  byte[] b = new byte[Convert.ToInt32(filesize)];
  // byte[] jpgcontent = (byte[])dt.Rows[0]["jpg"];
   
  // = _ValueBytes;
  //buf是保存该二进制流的变量,类型为byte []

  //filecontent = BitConverter.ToString(_ValueBytes);


  b = (byte[])read[0];
  FileStream fs = new FileStream(@"D:\c#\zhiban2\yuns.htm", FileMode.CreateNew);
  BinaryWriter bw = new BinaryWriter(fs);
  bw.Write(b, 0, Convert.ToInt32(filesize));

  fs.Close();
  bw.Close();
执行以上代码,还是写在了服务器端,
如何写文件直接写到客户端呢,在asp.net C#中,thks

------解决方案--------------------
出于ie或是ff的安全性,你可以用js去写,但是会有安全性的提示