Summary (3) - Upload Pictures to Database in ASP.NET

xiaoxiao2021-03-17  206

Summary (3) - Upload Pictures to Database in ASP.NET

Squire: Terrylee

One. Upload pictures to the database

1

/ ** /

///

2 /// Upload picture 3 ///

4

Private

Void

UploadFile ()

5

{6 / ** //// give users to upload the file name 7 string strFilePathName = loFile.PostedFile.FileName; 8 string strFileName = Path.GetFileName (strFilePathName); 9 int FileLength = loFile.PostedFile.ContentLength; 1011 if ( FileLength <= 0) 12 Return; 1314 / ** //// Upload File 15 TRY16 {17 18 / ** //// Image File Temporary Storage Byte Arrse 19 Byte [] FileByteArray = New Byte [filength "; 2021 / ** //// Establish data stream object 22 stream streamObject = Lofile.postedFile.inputStream; 2324 / ** //// Read image file data, FileByteArray is a data storage body, 0 is a data pointer location, Filelnegth the data length 25 StreamObject.Read (FileByteArray, 0, FileLength); 2627 / ** //// SQL Server to establish a link 28 string strCon = System.Configuration.ConfigurationSettings.AppSettings [ "DSN"]; 29 SqlConnection Con = new SqlConnection (strCon); 30 String sqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, imageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)"; 31 SqlCommand CmdObj = new SqlCommand (sqlCmd, Con); 32CmdObj.Parameters.Add ( "@ Image", SqlDbType.Binary, FileLength) .Value = FileByteArray; 33 CmdObj.Parameters.Add ( "@ ContentType", SqlDbType.VarChar, 50) .Value = loFile.PostedFile.ContentType; / / Record file type 34 35 / ** /// / Other single table data record upload 36 cmdobj.Parameters.add ("@ imagedescription", sqldbtype.varchar, 200) .value = tbdescription.text; 37 38 / ** //// Record the file length, use 39 cmdobj.Parameters.add ("@ imagesize", sqldbtype.bigint, 8) .value = filelength; 40 con - (); 41 cmdobj.executenonquery (); 42 C. close ();

4344 / ** //// Jump page 45 response.Redirect ("Showall.aspx"); 46} 47 Catch (Exception EX) 48 {49 Throw EX; 50} 51} II. Read the image from the database

1

/ ** /

///

2 /// Display picture 3 ///

4

Private

Void

Showimages ()

5

{6 / ** //// ID for image ID 7 int imgid = convert.Toint32 (Request.QueryString ["ID"]); 8 9 / ** //// Establishing Database connection 10 string string strCon = system.configuration .ConfigurationSettings.AppSettings [ "DSN"]; 11 SqlConnection Con = new SqlConnection (strCon); 12 String sqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID"; 13 SqlCommand CmdObj = new SqlCommand (sqlCmd, Con); 14 CmdObj .Parameters.add ("@ imageid", sqldbtype.int) .Value = imgid; 15 16 Con. 17 SqlDataReader Sqlreader = cmdobj.executeReader (); 18 SQLReader.read (); 1920 / ** // // Set the output file type 21 response.contentType = (string) SQLReader ["ImageContentType"]; 22 23 / ** //// Output image file binary number 24 response.outputStream.write ((byte []) Sqlreader ["ImageData"], 0, (int) SQLReader ["ImageSize"]); 25 response.end (); 2627con.close (); 28}

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

New Post(0)