ASP.NET to upload files to database

xiaoxiao2021-03-06  19

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:

title

Required "ControlTovAlidate =" txttitle "> * Required



DOCUTMENT to UPLOAD



Second step

We can save uploaded files to the cache, the size of the cache and the specific size of the file, we can use the following code to obtain the specific size of the file:

INT INTDDOCLEN = txtFileContents.postedFile.contentLength;

Then, we can set the specific size of the cache:

Byte [] docbuffer = new byte [intdoclen];

After setting this, we can save the content of the upload file to the cache:

Stream objstream;

Objstream = txtFileContents.postedFile.InputStream;

Objstream.read (Docbuffer, 0, INTDOCLEN);

In the above code, when the cache is read, starting from the cache 0 position until the length of the entire file, this is the size of the entire file or the entire cache.

third step

Now we need to save the cache data to the database, we have until the data table structure, so we can implement this feature by writing a simple SQL statement. In the above content, we have written a stored procedure. In the program, we only have a SQLCommand object and pass this stored procedure to it, and set the "@Doc" parameter to get the cache data: cmduploaddoc = New SQLCommand "USP_BOOKSUPLOADFILE", BOOKSCONN);

cmduploaddoc.commandtype = commandtype.storedProcedure;

CmduploadDoc.Parameters.Add ("@ title", Sqldbtype.varchar, 200);

CmduploadDoc.Parameters.Add ("@ doc", sqldbtype.image;

CmduploadDoc.Parameters.Add ("@ doctype", sqldbtype.varchar, 4);

CmduploadDoc.parameters [0] .value = txtttitle.text;

CmduploadDoc.Parameters [1] .value = docbuffer;

CmduploadDoc.Parameters [2] .value = strdoctype;

Click the button to process the code

Private void btnsubmit_click (Object Sender, System.Eventargs E)

{

String strdocext;

// strDrDOCTYPE is used to save the type of uploaded file

String strdoctype;

// Used to save the file size

Int INTDDOCLEN

// stream is used to read upload data

Stream objstream;

SqlConnection booksconn;

Sqlcommand cmduploaddoc;

IF (isvalid)

{

IF (txtfilecontents.postedfile! = null)

{

//file type

strDocext = cstring.right

(txtfilecontents.postedfile.filename, 4) .tolower ();

Switch (strDocext)

{

Case ".doc":

StrDrDOCTYPE = "DOC";

Break;

Case ".ppt":

strDDYPE = "PPT";

Break;

Case ".htm":

StrDrDOCTYPE = "htm";

Break;

Case ".html":

StrDrDOCTYPE = "htm";

Break;

Case ".jpg":

strDype = "jpg";

Break;

Case ".gif":

StrDOctype = "gif";

Break;

DEFAULT:

StrDrDype = "txt";

Break;

}

// Upload the specific content of the file

INTDDOCLEN = txtfilecontents.postedfile.contentLENGTH;

Byte [] docbuffer = new byte [intdoclen];

Objstream = txtFileContents.postedFile.InputStream;

// File Save to Cache

// Cache will be saved to the database

Objstream.read (Docbuffer, 0, INTDOCLEN);

Booksconn = new

SqlConnection ("Server = Server; UID = SA; Database = BOOKS");

cmduploaddoc = new

Sqlcommand ("USP_BOOKSUPLOADFILE", BOOKSCONN);

cmduploaddoc.commandtype = commandtype.storedProcedure;

CmduploadDoc.Parameters.Add ("@ title", Sqldbtype.varchar, 200);

CmduploadDoc.Parameters.Add ("@ doc", sqldbtype.image;

CmduploadDoc.Parameters.Add ("@ doctype", sqldbtype.varchar, 4);

CmduploadDoc.parameters [0] .value = txtttitle.text;

CmduploadDoc.Parameters [1] .value = docbuffer;

CmduploadDoc.Parameters [2] .value = strdoctype;

Booksconn.open ();

CmduploadDoc.executenonquery ();

Booksconn.close ();

}

}

}

to sum up

The method we mentioned above is suitable for all types of files, making appropriate modifications to the above code, we can create a fully database-based file management system.

转载请注明原文地址:https://www.9cbs.com/read-40266.html

New Post(0)