Why do you want to save the file to a database? Many reasons, the most direct is that the file into the database, you can better management of documents, especially text files, pictures, etc., if not using the database, a large number of times, it is difficult to achieve effective management and differences. In particular, it is necessary to do some of the relevant applications, the file into the database is the best choice for the word processing, retrieval, can directly use some of the database functions, can be truly effective management. This article is mainly for text based files, such as word, but, in fact, a slight modification of the program, you can upload all file types.
Data table structure
Now, let's look at the structure of the database table where the files are stored, and here we give the standard SQL statements that set up the table:
CREATE TABLE Tblbooksupload
(
DocID int not NULL IDENTITY Primary Key,
DocTitle varchar (200),
Doc image,
DocType varchar (50),
EntryDate datetime Default GetDate ()
)
In the above statement, we see that the data table Tblbooksupload contains five fields:
Field DocId is the key field of the table, the data record number;
Field DocTitle is used to simply explain the upload file, if the upload text file, we generally set it to file title, image, program, etc., set for the image, the simple introduction of the program;
Field Doc is the field that holds the file we uploaded, note that the Doc field is set to the image category.
Field doctype is used to save the type of files we upload, maybe we wonder why we want this field? In fact, this field is very important, when users get data from the database, this field will be used to specify the data field Doc data category, then, the browser based on this field to determine the data presented to the user;
Field DateTime is a time field and we can see that the value of the field is taken from the current date of the server.
Here is the stored procedure that inserts the data, let's look at the specific code:
CREATE PROCEDURE Usp_booksuploadfile
@Title varchar (200),
@Doc image,
@DocType varchar (4)
As
INSERT Tblbooksupload (Doctitle,doc,doctype)
VALUES (@Title, @Doc, @DocType)
Go
Steps to upload files
Now, we start from the text of the upload file to the database of the specific steps, and then from the code to achieve:
First, get the uploaded file from the client, and then we'll put it into the data stream;
Second, the server side reads the data stream and then saves it to the cache;
Third, save the cached data to the database;
Now, let's take a step-by-step look at how to implement these features in a program.
First step
Of course, first of all we want to implement users in the browser-side free to select files, and then upload, where the user select files, of course, the standard Windows requirements, so we use the form of the file component to the user to select files. Note that because the upload file, so, in the form of the property settings, we should be set to: Multipart/form-data, so that you can upload files correctly. Here is the main code for uploading the page:
We can save uploaded files through the data stream to the cache, the size of the cache and the exact size of the file, we can use the following code to get the exact size of the file:
int intdoclen = TxtFileContents.PostedFile.ContentLength;
Then, we can set the specific size of the cache:
byte[] Docbuffer = new Byte[intdoclen];
After this setting, we can save the contents of the uploaded file to the cache:
In the above code, when reading the cache, start with the cached 0-bit, until the length of the entire file, which is actually the size of the entire file or the entire cache.
Third Step
Now all we have to do is save the cached data to the database, and we've been up to the data table structure, so that we can do this by writing a simple SQL statement. In the above, we write a stored procedure in which we simply create the SqlCommand object and pass the stored procedure to it and set the "@Doc" parameter to get the cached data:
Cmduploaddoc = new SqlCommand ("Usp_booksuploadfile", booksconn);
Cmduploaddoc.parameters[0]. Value = Txttitle.text;
CMDUPLOADDOC.PARAMETERS[1]. Value = Docbuffer;
CMDUPLOADDOC.PARAMETERS[2]. Value = Strdoctype;
Booksconn.open ();
Cmduploaddoc.executenonquery ();
Booksconn.close ();
}
}
}
Summarize
The method we mentioned above, suitable for all types of files, the above code to make appropriate changes, we can build a completely database based file management system.
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