Introduction
Why save files to the database? There are many reasons, the most straightforward is, after putting the file into the database, you can make a better management, especially text files, pictures, etc. If you don't use the database, it is difficult to do effectively when it is huge. the difference. In particular, it is necessary to make some applications related to text, and put all the files in the database is the best choice. The processing, search, etc. of the text can directly utilize some of the functions of the database, which can be truly effective management. . This paper is primarily targeted by text based files, such as Word, etc., but actually modify the program, you can upload all file types.
Data table structure
Now let's see the database table structure of the store, here, we give the standard SQL statement for the establishment 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 briefly describe the upload file, if you upload text files, we generally set it as a file title, image, program, etc., set to an image, a brief introduction to the program;
Field DOC is the field used to store the files we upload, note that the settings of the DOC field are image categories here;
Field DOCTYPE is used to save the type of our upload file, may we why should we we want this field? In fact, this field is very important. When the user gets data from the database, this field will be used to specify the category of data in the data field DOC, and then the browser determines the data presented to the user according to this field;
Field DateTime is a time field, we can see the value of this field to take the current date of the server.
Below is a stored procedure inserted into the data, let's see 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
Upload file steps
Now, let's take a look at the specific steps of uploading files to the database, and then implement it from the code:
First, get the uploaded file from the client, then we put it into the data stream;
Second, the server reads the data stream and saves it to the cache;
Third, save the cache data to the database;
Now, let's see how to implement these features in the program.
first step
Of course, first we want to implement the user freely select the file in the browser, then upload, the user selects the file, which is of course required a standard Windows mode, so we use the FORM file component to select files to users. Note that because the file is uploaded, it should be set to: Multipart / Form-Data when the Form's property is set, so that the file can be uploaded correctly. Here is the main code of the uploaded page: