本文修改来源:http://www.cnblogs.com/zmgdpg/archive/2005/03/31/129758.html
效果:
数据库:
思路:
首先保存word到数据库:获取上传文件字节的大小,然后从流中读取字节,其次把获得的流保存到数据库。
读取数据库:根据名称查找出来数据库中的流数据,然后用读取器BinaryWriter读取流文件保存到指定的目录下面。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration; namespace WordToDB
{
public partial class WrodToDB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void btn_Click(object sender, EventArgs e)
{
/***************保存word到数据库**********/
string name = tb1.Text;
//接收上传文件
Stream fileStream = FileUpload1.PostedFile.InputStream;
//获取上传文件字节的大小
int length = FileUpload1.PostedFile.ContentLength;
byte[] wordData = new byte[length];
//从流中读取字节并写入wordData
fileStream.Read(wordData, , length);
//获取当前时间
DateTime time = DateTime.Now;
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLStr"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO word (fileName,postTime,fileContent) values (@fileName,@postTime,@fileContent)";
SqlParameter nameParam = new SqlParameter("@fileName", System.Data.SqlDbType.VarChar, );
nameParam.Value = name;
cmd.Parameters.Add(nameParam);
SqlParameter timeParam = new SqlParameter("@postTime", System.Data.SqlDbType.DateTime, );
timeParam.Value = time;
cmd.Parameters.Add(timeParam);
//添加word文件
SqlParameter contentParam = new SqlParameter("@fileContent", System.Data.SqlDbType.Image);
contentParam.Value = wordData;
cmd.Parameters.Add(contentParam);
conn.Open();
int i = cmd.ExecuteNonQuery();
if (i > )
{
Response.Write("<script>alert('上传成功')</script>");
}
else
{
Response.Write("<script>alert('上传失败')</script>");
}
conn.Close();
} protected void btn1_Click(object sender, EventArgs e)
{
/****************读取数据库中的流文件**********************/
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLStr"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//根据TextBox中指定的文件名进行查找读取
cmd.CommandText = "select fileContent from word where fileName='" + tb1.Text.ToString() + "'";
FileStream fs;
BinaryWriter bw;
//设定允许读取到缓冲区的最大长度
int buffersize = ;
//要将字节流读入的缓冲区
byte[] outbyte = new byte[buffersize];
//用于记录已经读取的字节数
long reval;
//字段中的索引,从这里开始读取操作
long startIndex;
//FileStream对象将封装的文件的相对路径或绝对路径
string filePath = @"C:\" + tb1.Text + ".doc";
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = ;
//将字节流读入outbyte缓冲区中并返回读取的字节数
reval = reader.GetBytes(, startIndex, outbyte, , buffersize);
//当读取的字节流达到缓冲区允许的最大长度时要卸载缓冲区内的数据并将数据写入文件
while (reval == buffersize)
{
bw.Write(outbyte);
bw.Flush();
//重新设定开始读取的位置,并继续读取和写数据
startIndex += buffersize;
reval = reader.GetBytes(, startIndex, outbyte, , buffersize);
}
//将缓冲区内最后剩余的数据写入文件
bw.Write(outbyte, , (int)reval - );
bw.Flush();
bw.Close();
fs.Close();
}
if (reader.Read().ToString() != "")
{
Response.Write(@"<script>alert('读取成功! word的位置在:C:\\" + tb1.Text + ".doc')</script>");
}
else
{
Response.Write("<script>alert('读取失败')</script>");
}
reader.Close();
conn.Close(); }
}
}