我做了一个将文件以2进制存储到数据库的程序,例如上传文件,会记录文件的类型 例如 word excel然后再将此文件2进制保存到SQLSERVER中,读取的时候,根据记录的类型生成指定的类型的文件,提供给用户下载,请问 我读取可以获取存储的byte[]数组,但不清楚如何生成文件提供给用户下载,请大侠们指点围观,多谢。

解决方案 »

  1.   

    File.WriteAllBytes(@"文件的完全限定名",byte数组);
      

  2.   


                string sPath = "D:\\";//路径
                string FileLoad = sPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";//.jpg-.doc   -.xls
                FileStream fs = new FileStream(FileLoad, FileMode.Create, FileAccess.Write);//创建图片存放路径
                BinaryWriter bw = new BinaryWriter(fs);
                while (i < bytes.Length)
                {
                    bw.Write(bytes);
                    i++;
                }
      

  3.   

    代码来自:
    http://digga2012.iteye.com/blog/317530
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]); SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn); DataSet ds=new DataSet(); da.Fill(ds,"table1"); DataTable dt=ds.Tables["table1"]; string name=System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString() DateTime.Today.ToString("yyyyMMdd") new Random(DateTime.Now.Millisecond).Next(10000).ToString() ".csv";//存放到web.config中downloadurl指定的路径,文件格式为当前日期 4位随机数 FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write); StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312")); sw.WriteLine("自动编号,姓名,年龄"); foreach(DataRow dr in dt.Rows) { sw.WriteLine(dr["ID"] "," dr["vName"] "," dr["iAge"]); } sw.Close(); Response.AddHeader("Content-Disposition", "attachment; filename=" Server.UrlEncode(name)); Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载 Response.WriteFile(name); // 把文件流发送到客户端 Response.End(); Response.BinaryWrite写Byte[],不需要保存成文件
      

  4.   


                Response.Clear();
                Response.BufferOutput = false;
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                string fineName = "test";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fineName + ".xls");
                Response.ContentType = "application/ms-excel";
                byte[] buffer = new byte[300];
                Response.OutputStream.Write(buffer, 0, buffer.Length);
                Response.Close();