【asp源码栏目提醒】:网学会员asp源码为您提供在ASP NET程序中存储和调用word文件 - 培训资料参考,解决您在在ASP NET程序中存储和调用word文件 - 培训资料学习中工作中的难题,参考学习。
本文通过一个实例概要讲解如何在
ASP.NET程序中配合SQL Server2000进行word文件的存储和调用过程没有使用VBA 。
1 建立数据库 首先,我们在数据库中建立一个表,表中有三个字段,fileNamevarchar50,postTimedatetime8 fileContentimage16分别存储文件名称,上传时间和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 2 上传并存储word文件 在VS.NET中建立一个
ASP.NET web应用程序,在界面内加入如下控件 控件类型 ID Text 说明 Label Label1 请输入文档的标题 Label Label2 请选择具体文档 File Field File1 上传控件要将此HTML控件转化为服务器控件 TextBox name_TextBox 用于录入文档标题 Button Btn_OK 上传文件 Button Btn_get 读取文件 HyperLink HyperLink1 打开 用于打开word文档 上传文件时首先通过上传控件找到所需上传的文件,然后获取文件的大小,最后以流的形式写入数据库,具体代码为: private void Btn_OK_Clickobject sender System.EventArgs e string namename_TextBox.Text //接收上传文件 Stream fileStreamFile1.PostedFile.InputStream //获取上传文件字节的大小 int lengthFile1.PostedFile.ContentLength byte wordDatanew bytelength //从流中读取字节并写入wordData int nfileStream.ReadwordData0length //获取当前时间 DateTime timeDateTime.Now //连接数据库 SqlConnection connnew SqlConnection conn.ConnectionStringworkstation idTIANCHUNZHUpacket size4096integrated securitySSPIdata sourceTIANCHUNZHUpersist security infoFalseinitial catalogtest SqlCommand cmdnew SqlCommand cmd.Connectionconn cmd.CommandTextINSERT INTO word fileNamepostTimefileContent values fileNamepostTimefileContent SqlParameter nameParamnew SqlParameterfileNameSystem.Data.SqlDbType.VarChar50 nameParam.Valuename cmd.Parameters.AddnameParam SqlParameter timeParamnew SqlParameterpostTimeSystem.Data.SqlDbType.DateTime8 timeParam.Valuetime cmd.Parameters.AddtimeParam //添加word文件 SqlParameter contentParamnew SqlParameterfileContentSystem.Data.SqlDbType.Image ①//见本段最后注解 contentParam.ValuewordData cmd.Parameters.AddcontentParam conn.Open cmd.ExecuteNonQuery conn.Close 注①:此处由于是Image类型文件,事先可能无法预测文件的大小,因此可不必指定size参数。
如果希望控制上传文件的大小则可以输入size参数。
如指定1000,则上传时最大可以上传1k的word文档。
(3) 从数据库中读取数据并恢复为word文件 读取数据时先将数据从数据库中读入缓冲区,然后再从缓冲区写入最终文件。
因此首先要开辟一个缓冲区并设定它的大小,每当缓冲区读满时就要将缓冲区内的数据写入文件,以清空缓冲区并继续向缓冲区读数据,直到最后一次将缓冲区内剩余的数据全部写入文件,新的word文档即可生成。
由于这一部分用到了字节流的输入输出操作,因此要引用System.IO命名空间 下面是关于这一部分的完整代码: private void Btn_get_Clickobject sender System.EventArgs e //连接数据库 SqlConnection connnew SqlConnection conn.ConnectionStringworkstation idTIANCHUNZHUpacket size4096integrated securitySSPIdata sourceTIANCHUNZHUpersist security infoFalseinitial catalogtest SqlCommand cmdnew SqlCommand cmd.Connectionconn //根据TextBox中指定的文件名进行查找读取 cmd.CommandTextselect fileContent from word where fileNamename_TextBox.Text.ToString FileStream fs BinaryWriter bw //设定允许读取到缓冲区的最大长度 int buffersize100 //要将字节流读入的缓冲区 byte outbytenew bytebuffersize //用于记录已经读取的字节数 long reval //字段中的索引,从这里开始读取操作 long startIndex //FileStream对象将封装的文件的相对路径或绝对路径 string filePathC:wordData.doc conn.Open SqlDataReader reader readercmd.ExecuteReader while reader.Read fsnew FileStreamfilePathFileMode.OpenOrCreateFileAccess.Write bwnew BinaryWriterfs startIndex0 //将字节流读入outbyte缓冲区中并返回读取的字节数 revalreader.GetBytes0startIndexoutbyte0buffersize //当读取的字节流达到缓冲区允许的最大长度时要卸载缓冲区内的数据并将数据写入文件 while revalbuffersize bw.Writeoutbyte bw.Flush //重新设定开始读取的位置,并继续读取和写数据 startIndexbuffersize revalreader.GetBytes0startIndexoutbyte0buffersize //将缓冲区内最后剩余的数据写入文件 bw.Writeoutbyte0intreval-1 bw.Flush bw.Close fs.Close reader.Close conn.Close 此时将按照filePath中指定的路径和名称重新生成word文档。
可以在filePath中根据具体情况指定生成的word文档的名称和路径。
(4) 打开word文档 在打开word文档这一部分暂时并没有找到通过Button按钮直接打开word的有效办法,但我们可以HyperLink控件,只要将HyperLink控件的NavigateUrl属性指向word文档的物理路径就可以了。