点滴积累【C#】---C#实现上传word以流形式保存到数据库和读取数据库中的word文件。
本文修改来源:http://www.cnblogs.com/zmgdpg/archive/2005/03/31/129758.html
效果:
数据库:
思路:
首先保存word到数据库:获取上传文件字节的大小,然后从流中读取字节,其次把获得的流保存到数据库。
读取数据库:根据名称查找出来数据库中的流数据,然后用读取器BinaryWriter读取流文件保存到指定的目录下面。
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 using System.Data.SqlClient; 9 using System.Configuration; 10 11 namespace WordToDB 12 { 13 public partial class WrodToDB : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 18 } 19 20 protected void btn_Click(object sender, EventArgs e) 21 { 22 /***************保存word到数据库**********/ 23 string name = tb1.Text; 24 //接收上传文件 25 Stream fileStream = FileUpload1.PostedFile.InputStream; 26 //获取上传文件字节的大小 27 int length = FileUpload1.PostedFile.ContentLength; 28 byte[] wordData = new byte[length]; 29 //从流中读取字节并写入wordData 30 fileStream.Read(wordData, 0, length); 31 //获取当前时间 32 DateTime time = DateTime.Now; 33 //连接数据库 34 SqlConnection conn = new SqlConnection(); 35 conn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLStr"].ToString(); 36 SqlCommand cmd = new SqlCommand(); 37 cmd.Connection = conn; 38 cmd.CommandText = "INSERT INTO word (fileName,postTime,fileContent) values (@fileName,@postTime,@fileContent)"; 39 SqlParameter nameParam = new SqlParameter("@fileName", System.Data.SqlDbType.VarChar, 50); 40 nameParam.Value = name; 41 cmd.Parameters.Add(nameParam); 42 SqlParameter timeParam = new SqlParameter("@postTime", System.Data.SqlDbType.DateTime, 8); 43 timeParam.Value = time; 44 cmd.Parameters.Add(timeParam); 45 //添加word文件 46 SqlParameter contentParam = new SqlParameter("@fileContent", System.Data.SqlDbType.Image); 47 contentParam.Value = wordData; 48 cmd.Parameters.Add(contentParam); 49 conn.Open(); 50 int i = cmd.ExecuteNonQuery(); 51 if (i > 0) 52 { 53 Response.Write("<script>alert('上传成功')</script>"); 54 } 55 else 56 { 57 Response.Write("<script>alert('上传失败')</script>"); 58 } 59 conn.Close(); 60 } 61 62 protected void btn1_Click(object sender, EventArgs e) 63 { 64 /****************读取数据库中的流文件**********************/ 65 //连接数据库 66 SqlConnection conn = new SqlConnection(); 67 conn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLStr"].ToString(); 68 SqlCommand cmd = new SqlCommand(); 69 cmd.Connection = conn; 70 //根据TextBox中指定的文件名进行查找读取 71 cmd.CommandText = "select fileContent from word where fileName='" + tb1.Text.ToString() + "'"; 72 FileStream fs; 73 BinaryWriter bw; 74 //设定允许读取到缓冲区的最大长度 75 int buffersize = 100; 76 //要将字节流读入的缓冲区 77 byte[] outbyte = new byte[buffersize]; 78 //用于记录已经读取的字节数 79 long reval; 80 //字段中的索引,从这里开始读取操作 81 long startIndex; 82 //FileStream对象将封装的文件的相对路径或绝对路径 83 string filePath = @"C:\" + tb1.Text + ".doc"; 84 conn.Open(); 85 SqlDataReader reader; 86 reader = cmd.ExecuteReader(); 87 while (reader.Read()) 88 { 89 fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); 90 bw = new BinaryWriter(fs); 91 startIndex = 0; 92 //将字节流读入outbyte缓冲区中并返回读取的字节数 93 reval = reader.GetBytes(0, startIndex, outbyte, 0, buffersize); 94 //当读取的字节流达到缓冲区允许的最大长度时要卸载缓冲区内的数据并将数据写入文件 95 while (reval == buffersize) 96 { 97 bw.Write(outbyte); 98 bw.Flush(); 99 //重新设定开始读取的位置,并继续读取和写数据 100 startIndex += buffersize; 101 reval = reader.GetBytes(0, startIndex, outbyte, 0, buffersize); 102 } 103 //将缓冲区内最后剩余的数据写入文件 104 bw.Write(outbyte, 0, (int)reval - 1); 105 bw.Flush(); 106 bw.Close(); 107 fs.Close(); 108 } 109 if (reader.Read().ToString() != "") 110 { 111 Response.Write(@"<script>alert('读取成功! word的位置在:C:\\" + tb1.Text + ".doc')</script>"); 112 } 113 else 114 { 115 Response.Write("<script>alert('读取失败')</script>"); 116 } 117 reader.Close(); 118 conn.Close(); 119 120 } 121 } 122 }