. NET how to store PDFs, Word, and Excel into a database

Source: Internet
Author: User
This article is mainly for everyone to introduce the ASP. NET save PDF, Word and Excel files to the database of relevant information, with a certain reference value, interested in small partners can refer to

In the project, sometimes we need to upload PDFs, Word and Excel documents and so on to the database for later use. This article explains how to save these files to the database today.

Detailed steps

First step: Open the database, click New Query, and create a table called documents:

The code is as follows:


CREATE TABLE Documents (SNo int identity, Name_file varchar (+), DisplayName varchar (), Extension varchar (ten), Content Type varchar ($), FileData varbinary (max), FileSize bigint, Uploaddate datetime)

This table contains the data:

Sno Serial Number

Name_file file name

Name of the DisplayName file display

Extension file extension

ContentType file type

Filedata File binary format

FileSize File Size

Uploaddate File Import Time

Step Two: open visual Studio and create a new empty Web site named "Filestobinary"

Step Three: Add a new page, named "Conversion.aspx"

On this page we need to add a textbox, FileUpload, button to these three controls.

Design interface

Of course you can also enter the following code directly in the CONVERSION.APSX file:


Show Files <asp:textbox id= "Txtfilename" runat= "Server" >  </asp:TextBox> <br/>  Select File <asp: FileUpload id= "FileUpload1" runat= "server"/> <br/> <asp:button  id= "Button1" runat= "Server" text= "import" onclick= "Button1_Click"/>

Fourth step: after the control is added, double-click the button to add the following namespace in the Conversion.apxs.cs file.


Using system;using system.web;using system.data.sqlclient;using system.data;using System.IO;

Then write the code in button1_click, convert the file to a binary stream, and click the button to save the file to the database.

The code is as follows:


protected void Button1_Click (object sender, EventArgs e) {if (!   Fileupload1.hasfile) {Response.Write ("File not selected"); 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);   Verify that the saved file name extension is Pdf,doc,docx,xls. if ((Extension = = ". pdf") | | (Extension = = ". Doc") | | (Extension = = ". docx") | |   (Extension = = ". xls")) {//Verify the size of the file if (file. ContentLength <= 31457280) {//Table Insert data using (SqlConnection conn = new SqlConnection 21015;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 ("Data has been added");    }} else {response.write ("Invalid file size"); return;}   } else {Response.Write ("Invalid file"); return; }   } }

Run results

Now browse the folder and you can add our files. Click Import to successfully Add.

If you select a file that does not conform to a rule, it displays:

Returns the database, when PDF, Word, and Excel files have been successfully added to the database.

"Recommended"

1. Special recommendation : "PHP Programmer Toolkit" V0.1 version download

2. asp free Video Tutorial

3. asp Tutorials

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.