Upload images to the database under ASP. NET and read the images

Source: Internet
Author: User

 

First, create an image storage database table in SQL Server. imagedata column is the binary data storage field of the image.

, Imagecontenttype column is the image file type record field, imagedescription column is the savings Graph

Description field of the image file. The imagesize column is the length field of the image file. 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]
*/

// The content of uploadimage. aspx is as follows:
<% @ Page inherits = "uploadimage. uploadimage" src = "uploadimage. cs"

Language = "C #" %>
<HTML> <title> upload an image </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> upload an image (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>
File description (add upload image description, such as author and 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>
//-------------------------------------------------------------------
// The content of the uploadimage. CS program is 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 control object
Protected textbox txtdescription;
Protected label txtmessage;
Protected int32 filelength = 0; // record file length variable
Protected void button_submit (system. Object sender, system. eventargs e ){
Httppostedfile upfile = up_file.postedfile; // httppostedfile object, used to read images

File Attributes
Filelength = upfile. contentlength; // record file length
Try {
If (filelength = 0) {// The file length is zero
Txtmessage. Text = "<B> select the file you want to upload </B> ";
} Else {
Byte [] filebytearray = new byte [filelength]; // temporary storage of byte Arrays for image files
Stream streamobject = upfile. inputstream; // create a data stream object
// Read image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
Streamobject. Read (filebytearray, 0, filelength );
// Create an 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 the file length, which is used for 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 image </B> "; // a message indicating that the image has been uploaded successfully
}
} Catch (exception ex ){
Txtmessage. Text = ex. Message. tostring ();
}}}}
//----------------------------------------------------------------------
// Well, the image has been uploaded to the database. What should I do now? Of course, it is read from the database and displayed on the web page,

See the following program:
// The ReadImage. aspx program content is as follows:
/-----------------------------------------------------------------------
<% @ Page inherits = "ReadImage. maindisplay" src = "ReadImage. cs" %>
//----------------------------------------------------------------------
// The ReadImage. CS program content is 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 is an image

ID
// Create 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

Component Type
// Binary number of output image files
Response. outputstream. Write (byte []) sqlreader ["imagedata"], 0,

(INT) sqlreader ["imagesize"]);
Response. End ();
Con. Close ();
// Very simple. ^_^
}
}
}
//--------------------------------------------------------------------
// Finally, we must display it on the web page.
// Showimage. hml
<HTML>
<Body>
This is the image read from the database:
<Body>
</Html>

From: http://www.nqqn.com/ym/178/59061.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.