點滴積累【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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM