How to use a FILE control, upload multiple attachments to the database at a time

zhaozj2021-02-16  52

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.web.ui.webcontrols; using system.web.ui.htmlControls; using system.io; using system.configuration;

Summary description of Namespace BJOFFICE {///

/// upfiles. /// public class upfiles: System.Web.UI.Page {protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI. WebControls.DropDownList files; protected System.Web.UI.HtmlControls.HtmlInputFile myFile; protected System.Web.UI.WebControls.Button Button3; protected static DataTable upfiles1; protected static SqlDataAdapter upfiles2;

public void CreatTable () {SqlConnection myConnection = new SqlConnection (ConfigurationSettings.AppSettings [ "ConnectionString"]); SqlDataAdapter myCommand = new SqlDataAdapter ( "select FileName, FileType, FileLen, FileData, [ID] from Accessories where [ID] <- 1 ", myconnection; dataset mydataset = new dataset (); mycommand.fill (MyDataSet," Accessoriestable "); Upfiles2 = mycommand; Upfiles1 = myDataSet.Tables [Accessoriestable"];}

Private void Page_load (Object Sender, System.EventArgs E) {// Place user code here to initialize page}

#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// private void InitializeComponent () {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.Button2.Click = new System.EventHandler (this.Button2_Click); this.Button3 .Click = new system.eventhandler (this.button3_click; this.load = new system.eventhandler (this.page_load);} #ENDREGION

private void Button1_Click (object sender, System.EventArgs e) {if (upfiles1 == null) {CreatTable ();} if (myFile.PostedFile.FileName = ""!) {string filename1 = Path.GetFileName (myFile.PostedFile. FileName); //if (Upfiles1.Rows.Find (FileName )==Null DataRow [] Myrow = Upfiles1.Select ("filename = '" filename1.tostring () "'"; if (Myrow.Length == 0) {int filelen = myFile.PostedFile.ContentLength; string filetype = myFile.PostedFile.ContentType; Stream fstream = myFile.PostedFile.InputStream; byte [] filedata = new byte [filelen]; fstream.Read (filedata, 0 , Filelen;

DATAROW DR = Upfiles1.NewRow (); DR [0] = filename1; DR [1] = fileten; DR [2] = filelen; dr [3] = fileData; Upfiles1.Rows.Add (DR); files.datasource = Upfiles1; files.databind ();

}} Else {page.registerStartupScript ("script", "