在ASP.NET程序中存储和调用word文件 | ||||||||||||||||||||||||||||||||
本文通过一个实例概要讲解如何在ASP.NET程序中配合SQL Server2000进行word文件的存储和调用过程(没有使用VBA )。
首先,我们在数据库中建立一个表,表中有三个字段,fileName(varchar,50),postTime(datetime,8), fileContent(image,16),分别存储文件名称,上传时间和word文件的具体内容,其中fileName为主键。具体的SQL脚本如下: CREATE TABLE [dbo].[word] ( [fileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [postTime] [datetime] NOT NULL , [fileContent] [image] NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
在VS.NET中建立一个ASP.NET web应用程序,在界面内加入如下控件
上传文件时首先通过上传控件找到所需上传的文件,然后获取文件的大小,最后以流的形式写入数据库,具体代码为: private void Btn_OK_Click(object sender, System.EventArgs e) { string name=name_TextBox.Text; //接收上传文件 Stream fileStream=File1.PostedFile.InputStream; //获取上传文件字节的大小 int length=File1.PostedFile.ContentLength; byte[] wordData=new byte[length]; //从流中读取字节并写入wordData int n=fileStream.Read(wordData,0,length); //获取当前时间 DateTime time=DateTime.Now; //连接数据库 SqlConnection conn=new SqlConnection(); conn.ConnectionString="workstation id=TIANCHUNZHU;packet size=4096;integrated security=SSPI;data source=TIANCHUNZHU;persist security info=False;initial catalog=test"; 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,50); nameParam.Value=name; cmd.Parameters.Add(nameParam); SqlParameter timeParam=new SqlParameter("@postTime",System.Data.SqlDbType.DateTime,8); 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(); cmd.ExecuteNonQuery(); conn.Close(); }
注①:此处由于是Image类型文件,事先可能无法预测文件的大小,因此可不必指定size参数。如果希望控制上传文件的大小则可以输入size参数。如指定1000,则上传时最大可以上传1k的word文档。
读取数据时先将数据从数据库中读入缓冲区,然后再从缓冲区写入最终文件。因此首先要开辟一个缓冲区并设定它的大小,每当缓冲区读满时就要将缓冲区内的数据写入文件,以清空缓冲区并继续向缓冲区读数据,直到最后一次将缓冲区内剩余的数据全部写入文件,新的word文档即可生成。 由于这一部分用到了字节流的输入输出操作,因此要引用System.IO命名空间 下面是关于这一部分的完整代码: private void Btn_get_Click(object sender, System.EventArgs e) { //连接数据库 SqlConnection conn=new SqlConnection(); conn.ConnectionString="workstation id=TIANCHUNZHU;packet size=4096;integrated security=SSPI;data source=TIANCHUNZHU;persist security info=False;initial catalog=test"; SqlCommand cmd=new SqlCommand(); cmd.Connection=conn; //根据TextBox中指定的文件名进行查找读取 cmd.CommandText="select fileContent from word where fileName='"+name_TextBox.Text.ToString()+"'"; FileStream fs; BinaryWriter bw; //设定允许读取到缓冲区的最大长度 int buffersize=100; //要将字节流读入的缓冲区 byte[] outbyte=new byte[buffersize]; //用于记录已经读取的字节数 long reval; //字段中的索引,从这里开始读取操作 long startIndex; //FileStream对象将封装的文件的相对路径或绝对路径 string filePath=@"C:wordData.doc"; conn.Open(); SqlDataReader reader; reader=cmd.ExecuteReader(); while (reader.Read()) { fs=new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.Write); bw=new BinaryWriter(fs); startIndex=0; //将字节流读入outbyte缓冲区中并返回读取的字节数 reval=reader.GetBytes(0,startIndex,outbyte,0,buffersize); //当读取的字节流达到缓冲区允许的最大长度时要卸载缓冲区内的数据并将数据写入文件 while (reval==buffersize) { bw.Write(outbyte); bw.Flush(); //重新设定开始读取的位置,并继续读取和写数据 startIndex+=buffersize; reval=reader.GetBytes(0,startIndex,outbyte,0,buffersize); } //将缓冲区内最后剩余的数据写入文件 bw.Write(outbyte,0,(int)reval-1); bw.Flush(); bw.Close(); fs.Close(); } reader.Close(); conn.Close(); } 此时将按照filePath中指定的路径和名称重新生成word文档。可以在filePath中根据具体情况指定生成的word文档的名称和路径。
在打开word文档这一部分暂时并没有找到通过Button按钮直接打开word的有效办法,但我们可以HyperLink控件,只要将HyperLink控件的NavigateUrl属性指向word文档的物理路径就可以了。 |