File upload source code

xiaoxiao2021-03-06  89

File upload here to upload to the server folder and database

1: Upload to folder

First look at the ASPX page:

code:

Below is an event triggered in the background, and the "File Upload" button:

code:

Private Void Submit_Click (Object Sender, System.EventArgs E)

{

Httppostedfile postedfile = this.up_file.postedfile; // Get the file to be uploaded

String FileName, FileExtension;

Filename = system.io.path.getFileName (PostedFile.FileName); // file name

IF (filename! = "")

{

FileExtension = system.io.path.Getextension (filename); // Upload file extension

String new_filename = fileExtension; // Rename the file

//postedfile.filename: Client file address

//postedfile.contenttype.tostring (): Uploaded file type

/ / Save the file to the folder, the address is in the Files folder in the same class directory of the current page.

PostedFile.saveas (System.Web.httpContext.current.Request.mAppath ("Files /") New_FileName);

// ..... You can save the corresponding information of the file to the database.

}

}

When displaying these file information, you can link the file address directly.

2: Upload to the database

code:

Private Void Submit_Click (Object Sender, System.EventArgs E)

{

Stream Imgstream;

Int Docsize;

String docname;

String IMGCONTENTTYPE;

String imguploadedname;

ImgStream = this.up_file.postedfile.inputStream; // binary flow DOCSIZE = this.up_file.postedFile.contentLength; // File size

imguploadedname = this.up_file.postedFile.FileName; // File Name

Byte [] docbody = new byte [DOCSIZE];

IMGCONTENTTYPE = this.up_file.postedfile.contentType; // File Type

DocName = imguploadedname.substring (imguploadedname.lastindexof ('//') 1);

INT N = ImgStream.read (DocBody, 0, DOCSIZE);

DataTable Temp = mynew.get_zh_engbriefing_byoid (0); // Return to an empty TABLE

DATAROW ROW = Temp.newrow ();

// ..... omitted other saving code

ROW ["Down_Path"] = DOCBODY; / / Save Data to Database, Down_Path field is binary

Row ["filename"] = docName; // save the file name

Row ["lx"] = imgconTenTtype; // Save file type

Temp.Rows.Add (Row);

Mynew.savezhengbriefing (TEMP);

}

When displaying these file information, you can refer to the following code if you want to implement the download:

1): When placed in the DataGrid, you must bind data to template columns, such as:

code:

Use DataGrid's itemCommand event to implement download

code:

PRIVATE VOID ENG_BRIEFING_ITEMCOMMAND (Object Source, System.Web.ui.WebControls.DataGridCommandEventArgs E) {

IF (e.commandname == "downfile")

{

Long Engbriefing_oid ​​= long.parse (engine_briefing.Items [E.Item.itemindex] .cells [0] .text.tostring ()); // First Return to the corresponding row to download data Record keyword OID

DataTable file = mynew.get_zh_engbriefing_byoid (Engbriefing_oid); // Get the file record in this file (here the get_zh_engbriefing_byoid method is my own defined according to OID)

IF (file.rows.count> 0)

{

Response.clear ();

Response.buffer = false;

Response.Appendheader ("Content-Disposition", "Attachment; FileName =" httputility.urlencode (file.rows [0] ["filename"]. TOSTRING (), system.text.encoding.utf8));

Response.binaryWrite ((byte []) file.rows [0] ["Down_Path"]);

Response.end ();

}

}

}

When the file information is displayed in a LinkButton, you can use the LLinkButton's onclick event to implement:

code:

code:

Private void filelink_click (Object Sender, System.EventArgs E)

{

Long engbriefing_oid ​​= long.parse (this.l_engbriefing_oid.text.tostring ()); // Get the corresponding OID of the line of the file

DataTable file = mynew.get_z_engbriefing_byoid (Engbriefing_oid); // Get the information in which the file is located

IF (file.rows.count> 0)

{

Response.clear ();

Response.buffer = false;

Response.Appendheader ("Content-Disposition", "Attachment; FileName =" httputility.urlencode (file.rows [0] ["filename"]. TOSTRING (), system.text.encoding.utf8));

Response.binaryWrite ((byte []) file.rows [0] ["Down_Path"]);

Response.end ();

}

}

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

New Post(0)