在项目中,有时候我们很须要把PDF、Word和Excel文档等等上传到数据库,以便往后运用。本日这篇文章向人人解说如何将这些文件保留到数据库的。
细致步骤
第一步:翻开数据库,单击新建查询,建立一个称号为Documents的表:
代码以下:
create table Documents ( SNo int identity, Name_File varchar(100), DisplayName varchar(50), Extension varchar(10), ContentType varchar(200), FileData varbinary(max), FileSize bigint, UploadDate datetime )
这个表包含了这些数据:
SNo序列号
Name_File文件名
DisplayName 文件显现的称号
Extension文件的扩展名
ContentType文件品种
FileData文件二进制花样
FileSize文件大小
UploadDate文件导入时候
第二步:翻开Visual Studio,新建一个空网站,定名为“FilesToBinary”
第三步:再增加一个新页面,定名为“Conversion.aspx”
在这个页面我们须要增加TextBox ,FileUpload ,Button这三个控件。
设想界面如图:
固然你也能够在Conversion.apsx文件直接输入以下代码:
显现文件 <asp:TextBox ID="txtfilename" runat="server"> </asp:TextBox> <br /> 挑选文件 <asp:FileUpload ID="FileUpload1" runat="server" /> <br /> <asp:Button ID="Button1" runat="server" Text="导入" OnClick="Button1_Click" />
第四步:控件增加后,双击Button,在Conversion.apxs.cs文件增加以下定名空间。
using System; using System.Web; using System.Data.SqlClient; using System.Data; using System.IO;
然后在Button1_Click编写代码,将文件转换为二进制流,点击Button后文件便可存到数据库中。
代码以下:
protected void Button1_Click(object sender, EventArgs e) { if (!FileUpload1.HasFile) { Response.Write("未挑选文件"); return; } else { string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); string extension = Path.GetExtension(filename); string contentType = FileUpload1.PostedFile.ContentType; HttpPostedFile file = FileUpload1.PostedFile; byte[] document = new byte[file.ContentLength]; file.InputStream.Read(document, 0, file.ContentLength); //考证保留的文件扩展名是不是为pdf,doc,docx,xls. if ((extension == ".pdf") || (extension == ".doc") || (extension == ".docx") || (extension == ".xls")) { //考证文件的大小 if (file.ContentLength <= 31457280) { //内外插进去数据 using (SqlConnection conn = new SqlConnection("Data Source=AFOD3-609221015;Initial Catalog=Personal;Integrated Security=True")) { conn.Open(); string sql = @"insert into Documents(Name_File,DisplayName,Extension,ContentType,FileData,FileSize,UploadDate) values(@Name_File,@DisplayName,@Extension,@ContentType,@FileData,@FileSize,getdate())"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@Name_File", SqlDbType.VarChar); cmd.Parameters["@Name_File"].Value = filename; cmd.Parameters.Add("@DisplayName", SqlDbType.VarChar); cmd.Parameters["@DisplayName"].Value = txtfilename.Text.Trim(); cmd.Parameters.Add("@Extension", SqlDbType.VarChar); cmd.Parameters["@Extension"].Value = extension; cmd.Parameters.Add("@ContentType", SqlDbType.VarChar); cmd.Parameters["@ContentType"].Value = contentType; cmd.Parameters.Add("@FileData", SqlDbType.VarBinary); cmd.Parameters["@FileData"].Value = document; cmd.Parameters.Add("@FileSize", SqlDbType.BigInt); cmd.Parameters["@FileSize"].Value = document.Length; cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); Response.Write("数据已增加"); } } else { Response.Write("文件大小无效"); return; } } else { Response.Write("无效文件"); return; } } }
运转效果如图:
这时候阅读文件夹,就能够增加我们的文件了。点击导入,胜利增加。
假如挑选了不符合划定规矩的文件后,则会显现:
返回数据库,这时候PDF、Word 和Excel文件已胜利增加到数据库啦。
【相干引荐】
1. 迥殊引荐:“php程序员工具箱”V0.1版本下载
2. ASP免费视频教程
3. ASP教程
以上就是.NET存储PDF、Word和Excel到数据库的要领详解的细致内容,更多请关注ki4网别的相干文章!