Home > Backend Development > C#.Net Tutorial > Detailed explanation of how .NET stores PDF, Word and Excel into databases

Detailed explanation of how .NET stores PDF, Word and Excel into databases

Y2J
Release: 2017-05-13 11:48:27
Original
2185 people have browsed it

This article mainly introduces the relevant information of ASP.NET to save PDF, Word and Excel files to the database in detail. It has certain reference value. Interested friends can refer to it

In projects, sometimes we need to upload PDF, Word and Excel documents, etc. to the database for future use. Today's article will explain to you how to save these files to the database.

Detailed steps

Step one:Open the database and click NewQuery, Create a table named Documents:

The code is as follows:


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 
)
Copy after login

This table contains these data:

SNo serial number

Name_File file name

DisplayName File display name

Extension file extension

ContentType file type

FileData file binary format

FileSize file size

UploadDate file import time

##Second step: Open Visual Studio, create a new empty website, name it "FilesToBinary"

Step 3:Add a new page and name it "Conversion.aspx"

In this page we need to add the three

controls of TextBox, FileUpload and Button. .

The design interface is as shown in the figure:

Of course, you can also directly enter the following code in the Conversion.apsx file:


显示文件
 <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" />
Copy after login

Step 4:After the control is added, double-click the Button and add the following namespace to the Conversion.apxs.cs file.


using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
Copy after login

Then write code in Button1_Click to convert the file into a binary stream. After clicking the Button, the file can be saved in the database.

The code is as follows:



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; 
   } 
  } 
}
Copy after login
The running result is as shown:

At this time Browse the folder and add our files. Click Import to add successfully.

If you select a file that does not meet the rules, it will display:

Return to the database, then PDF , Word and Excel files have been successfully added to the database.

【Related Recommendations】

1.

Special Recommendations:"php programmer tools "Box" V0.1 version download

2.

ASP free video tutorial

3.

ASP tutorial

The above is the detailed content of Detailed explanation of how .NET stores PDF, Word and Excel into databases. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template