溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

發(fā)布時(shí)間:2021-07-24 14:05:47 來(lái)源:億速云 閱讀:151 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

詳細(xì)步驟

第一步:打開數(shù)據(jù)庫(kù),單擊新建查詢,創(chuàng)建一個(gè)名稱為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 
)

這個(gè)表包含了這些數(shù)據(jù):

SNo序列號(hào)

Name_File文件名

DisplayName 文件顯示的名稱

Extension文件的擴(kuò)展名

ContentType文件種類

FileData文件二進(jìn)制格式

FileSize文件大小

UploadDate文件導(dǎo)入時(shí)間

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

第二步:打開Visual Studio,新建一個(gè)空網(wǎng)站,命名為“FilesToBinary”

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

第三步:再添加一個(gè)新頁(yè)面,命名為“Conversion.aspx”

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

在這個(gè)頁(yè)面我們需要添加TextBox ,F(xiàn)ileUpload ,Button這三個(gè)控件。

設(shè)計(jì)界面如圖:

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

當(dāng)然你也可以在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="導(dǎo)入" OnClick="Button1_Click" />

第四步:控件添加后,雙擊Button,在Conversion.apxs.cs文件添加以下命名空間。

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;

然后在Button1_Click編寫代碼,將文件轉(zhuǎn)換為二進(jìn)制流,點(diǎn)擊Button后文件便可存到數(shù)據(jù)庫(kù)中。

代碼如下:

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); 
 
   //驗(yàn)證保存的文件擴(kuò)展名是否為pdf,doc,docx,xls.
   if ((extension == ".pdf") || (extension == ".doc") || (extension == ".docx") || (extension == ".xls"))
   { 
 //驗(yàn)證文件的大小
    if (file.ContentLength <= 31457280)
    { 
     //表里插入數(shù)據(jù)
     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("數(shù)據(jù)已添加"); 
     } 
 
    } 
    else 
    { Response.Write("文件大小無(wú)效"); return; } 
   } 
   else 
   {
    Response.Write("無(wú)效文件"); return; 
   } 
  } 
}

運(yùn)行結(jié)果如圖:

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

這時(shí)瀏覽文件夾,就可以添加我們的文件了。點(diǎn)擊導(dǎo)入,成功添加。

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

如果選擇了不符合規(guī)則的文件后,則會(huì)顯示:

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

返回?cái)?shù)據(jù)庫(kù),這時(shí)PDF、Word 和Excel文件已經(jīng)成功添加到數(shù)據(jù)庫(kù)啦。

ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)

關(guān)于“ASP.NET如何保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI