1: Upload to the folder first look at the ASPX page: Program code:
Below is an event that is triggered in the background, and the "File Upload" button: Program Code: Private Void Submit_Click (Object Sender, System.EventArgs E) {httppostedfile postedfile = this.up_file.postedfile; // Get File String FileName, FileExtension; FileName = system.io.path.GetFileName (PostedFile.FileName); // file name if (fileEname! = ") {fileExtension = system.io.path.Getextension (filename); // Upload file extension String new_FileName = fileExtension; // Rename //postedFile.FileName: Client File Address //postedFile.ContentType.Tostring (): Uploaded file type // Save file to folder, address is the current page of the same class directory PostedFile.saves in the Files folder (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: uploaded to the database program 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 stream 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 a Empty Table DataRow Row = Temp.neWRow (); // ..... 省 省 其他 保 保] = DOCBODY; / / Save Data to Database, Down_Path field is binary row ["filename"] = DOCNAME; // Save file name row ["lx"] = imgcontentType; // save file type Temp.Rows.Add (row);} When displaying these file information, if you want to download, You can refer to the following code: 1): When placed in the DataGrid, you must bind data to template columns, such as program code:
ItemCommand event using datagrid download program code to implement: private void Eng_briefing_ItemCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {if (e.CommandName == "downFile") {long EngBriefing_OID = long.Parse (Eng_briefing .Items [E.Item.itemindex] .cells [0] .text.toString ()); // First Return the corresponding row record keyword OID DATATABLE File = mynew.get_zh_engbriefing_byoid (Engbriefing_oid); // Get The file record in the file (here the get_zh_engbriefing_byoid method is one of my own definitions to return Table) if (file.Rows.count> 0) {response.Rows.count> 0) {response.Rows.count> 0) {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]; response.end ();}}} When the file information is displayed in a LinkButton, you can use the LLinkButton's onclick event to implement: program code: Program Code: Private Void FileLink_Click (ibject sender, system.eventargs e) {long engbriefing_oid = long.parse (this.l_engbriefing_oid.text.tostring ()); // Get the corresponding Oid DataTable file in the row in the file = mynew.get_zh_Engbriefi Ng_BYOID (ENGBRIEFING_OID); // Gets the file information 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 ();}}