栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

ASP.NET保存PDF、Word和Excel文件到数据库

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ASP.NET保存PDF、Word和Excel文件到数据库

在项目中,有时候我们很需要把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文件直接输入下列代码:

显示文件
  
  

选择文件

第四步:控件添加后,双击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文件已经成功添加到数据库啦。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/55783.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号