Upload the image to the database under ASP.NET and read the code of for the image in detail
- 2020-05-10 18:02:12
- OfStack
First, a database table of image storage is established in SQL Server. ImageData Column is the image 2-base data storage field, ImageContentType Column is the image file type record field, and ImageDescription Column is the image storage chart
Like the file description field,ImageSize Column is the length field for storing image files. The structure is as follows:
UpLoadImage. aspx program contents are as follows:
UpLoadImage.cs program contents are as follows:
//----------------------------------------------------------------------
// ok, the image has been uploaded to the database. Now what should I do? Read and display in the database on page Web, of course,
See the following program:
ReadImage.aspx procedures are as follows:
Finally, of course, we'll display it on the Web page
ShowImage.hml
Like the file description field,ImageSize Column is the length field for storing image files. The structure is as follows:
CREATE TABLE [dbo].[ImageStore] (
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[ImageData] [image] NULL ,
[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageSize] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
UpLoadImage. aspx program contents are as follows:
<%@ Page Inherits="UploadImage.UploadImage" SRC="UpLoadImage.cs" Language="C#"%>
<HTML><title> To upload pictures </title>
<BODY bgcolor="#FFFFFF">
<FORM ENCTYPE="multipart/form-data" RUNAT="server" ID="Form1">
<TABLE RUNAT="server" WIDTH="700" ALIGN="left" ID="Table1" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD> To upload pictures ( Select the image you want to upload )</TD>
<TD>
<INPUT TYPE="file" ID="UP_FILE" RUNAT="server" STYLE="Width:320" ACCEPT="text/*" NAME="UP_FILE">
</TD>
</TR>
<TR>
<TD>
Document describing ( Add uploaded photo captions, such as: author, source )
</TD>
<TD>
<asp:TextBox RUNAT="server" WIDTH="239" ID="txtDescription" MAINTAINSTATE="false" />
</TD>
</TR>
<TR>
<TD>
<asp:Label RUNAT="server" ID="txtMessage" FORECOLOR="red" MAINTAINSTATE="false" />
</TD>
<TD>
<asp:Button RUNAT="server" WIDTH="239" onCLICK="Button_Submit" TEXT="Upload Image" />
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
UpLoadImage.cs program contents are as follows:
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace UploadImage
{
public class UploadImage : Page {
protected HtmlInputFile UP_FILE; //HtmlControl , WebControls The control object
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0; // Record the file length variable
protected void Button_Submit(System.Object sender, System.EventArgs e) {
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile Object to read image file properties
FileLength = UpFile.ContentLength; // Record file length
try {
if (FileLength == 0) { // File length is zero
txtMessage.Text = "<b> Please select the file you want to upload </b>";
} else {
Byte[] FileByteArray = new Byte[FileLength]; // Temporary storage of image files Byte An array of
Stream StreamObject = UpFile.InputStream; // Create a data flow image
// To read image file data, FileByteArray Is a data store, 0 Is the position of the data pointer, FileLnegth Is the data length
StreamObject.Read(FileByteArray,0,FileLength);
// To establish SQL Server link
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial
Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,
ImageDescription, ImageSize) valueS (@Image, @ContentType,
@ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).value =
FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).value =
UpFile.ContentType; // Record file type
// Upload other single table data records
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).value =
txtDescription.Text;
// Record file length, used when reading
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).value =
UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK! You have successfully uploaded your picture </b>";// Prompt successful upload
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}
//----------------------------------------------------------------------
// ok, the image has been uploaded to the database. Now what should I do? Read and display in the database on page Web, of course,
See the following program:
ReadImage.aspx procedures are as follows:
/-----------------------------------------------------------------------
<%@ Page Inherits="ReadImage.MainDisplay" SRC="ReadImage.cs"%>
//----------------------------------------------------------------------
//ReadImage.cs The program contents are as follows:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ReadImage {
public class MainDisplay : System.Web.UI.Page {
public void Page_Load(System.Object sender, System.EventArgs e) {
int ImgID = Convert.ToInt32(Request.QueryString["ImgID"]); //ImgID For the picture
ID
// Establish a database link
SqlConnection Con = new SqlConnection("Data Source=KING;Initial
Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];// Sets the output file type
// Output image file 2 Hexadecimal number system
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0,
(int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
// Very simple! ^_^
}
}
}
Finally, of course, we'll display it on the Web page
ShowImage.hml
<html>
<body>
This image is read from the database: <img src="ReadImage.aspx?ImgID=1">
<body>
</html>