ASP. NET saves PDF, Word, and Excel files to the database, asp. netpdf
In projects, sometimes we need to upload PDF, Word, and Excel documents to the database for future use. This article explains how to save these files to the database.
Detailed steps
Step 1:Open the database and click Create query to 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
)
This table contains the data:
SNo serial number
Name_File file name
Name displayed in the DisplayName File
Extension File Extension
ContentType file type
FileData file binary format
FileSize File Size
UploadDate File Import time
Step 2:Open Visual Studio and create an empty website named "FilesToBinary"
Step 3:Add a new page named "Conversion. aspx"
On this page, we need to add the TextBox, FileUpload, And Button controls.
Design Interface
You can also enter the following code in the Conversion. apsx file:
Show file
<asp: TextBox ID = "txtfilename" runat = "server">
</ asp: TextBox>
<br />
Select the file
<asp: FileUpload ID = "FileUpload1" runat = "server" />
<br />
<asp: Button ID = "Button1" runat = "server"
Text = "Import" OnClick = "Button1_Click" />
Step 4:After adding the control, double-click the Button to 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;
Then write code in button#click to convert the file into a binary stream. 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 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)
{
// Insert data in the table
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 ("Data has been added");
}
}
else
{Response.Write ("Invalid file size"); return;}
}
else
{
Response.Write ("Invalid file"); return;
}
}
}
Running result
In this case, you can browse the folder and add our files. Click Import.
If an invalid file is selected, the following information is displayed:
Return to the database. The PDF, Word, and Excel files are successfully added to the database.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.