Store images in binary format to the database
Core technologies:
Int filelen = This. fileupload1.postedfile. contentlength;
Byte [] filedata = new byte [filelen];
Httppostedfile HP = fileupload1.postedfile; // create an object for accessing the File Uploaded by the client
Stream sr = hp. inputstream; // create a data stream object
Sr. read (filedata, 0, filelen); // put the image data in the filedata array object instance. 0 indicates the starting position of the array pointer, filelen indicates the length of the stream to be read (the Ending position of the pointer ).....
1. Front-end
<Table cellpadding = "0" cellspacing = "0" style = "width: 290px">
<Tr>
<TD colspan = "2" style = "height: 30px">
<Asp: Label id = "label4" runat = "server" backcolor = "# c0ffff" font-size = "9pt" forecolor = "black"
TEXT = "Note: Only BMP, JPG, and GIF images can be saved"> </ASP: Label> </TD>
</Tr>
<Tr>
<TD style = "width: 66px; Height: 30px">
<Asp: Label id = "label1" runat = "server" font-size = "9pt" text = "select image"> </ASP: Label> </TD>
<TD align = "Left" style = "width: 100px; Height: 30px">
<Asp: fileupload id = "fileupload1" runat = "server" font-size = "9pt"/> </TD>
</Tr>
<Tr>
<TD style = "width: 66px; Height: 30px">
</TD>
<TD align = "Left" style = "width: 100px; Height: 30px">
<Asp: button id = "button1" runat = "server" font-size = "9pt" onclick = "button#click" text = "save"
Width = "66px"/> </TD>
</Tr>
<Tr>
<TD style = "width: 66px; Height: 30px">
</TD>
<TD align = "Left" style = "width: 100px; Height: 30px">
<Asp: Label id = "label3" runat = "server" font-size = "9pt" width = "216px"> </ASP: Label> </TD>
</Tr>
</Table>
2. Background
Using system. IO;
Using system. Data. sqlclient;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
Try
{
String imgpath = fileupload1.postedfile. filename;
String imgname = imgpath. substring (imgpath. lastindexof ("\") + 1 );
String imgextend = imgpath. substring (imgpath. lastindexof (".") + 1 );
If (! (Imgextend = "BMP" | imgextend = "jpg" | imgextend = "GIF "))
{
Label3.text = "the format of the uploaded image is incorrect! ";
Return;
}
Int filelen = This. fileupload1.postedfile. contentlength;
Byte [] filedata = new byte [filelen];
Httppostedfile HP = fileupload1.postedfile; // create an object for accessing the File Uploaded by the client
Stream sr = hp. inputstream; // create a data stream object
Sr. read (filedata, 0, filelen); // put the image data in the filedata array object instance. 0 indicates the starting position of the array pointer, filelen indicates the length of the stream to be read (the Ending position of the pointer)
Sqlconnection con = new sqlconnection ("Server = (local); User ID = sa; Pwd =; database = db_07 ");
Con. open ();
Sqlcommand COM = new sqlcommand ("insert into tb_15 (name) values (@ imgdata)", con );
Com. Parameters. Add ("@ imgdata", sqldbtype. Image );
Com. Parameters ["@ imgdata"]. value = filedata;
Com. executenonquery ();
Label3.text = "saved successfully! ";
}
Catch (Exception error)
{
Label3.text = "processing failed! Cause: "+ error. tostring ();
}
}
}
Read and display Binary Images in the database
core technology:
using system. data. sqlclient;
using system. io;
using system. drawing;
.....
memorystream MS = new memorystream (byte []) Dr ["name"]);
bitmap image = new Bitmap (MS );
string filepath = server. mappath ("files/");
directoryinfo dir = new directoryinfo (filepath);
fileinfo [] filecount = dir. getfiles ();
int I = filecount. length;
imagename = filepath + (I + 1) + ". jpg ");
image. save (imagename);
1. Front-end
<Table cellpadding = "0" cellspacing = "0" style = "width: 214px">
<Tr>
<TD style = "width: 45px; Height: 30px"> </TD>
<TD align = "Left" style = "width: 130px; Height: 30px">
<Asp: button id = "button1" runat = "server" font-size = "9pt" onclick = "button#click" text = "read and display"
Width = "100px"/> </TD>
</Tr>
<Tr>
<TD style = "width: 45px; Height: 30px">
<Asp: Label id = "label1" runat = "server" font-size = "9pt" text = "record No." width = "52px"> </ASP: label> </TD>
<TD align = "Left" style = "width: 130px; Height: 30px">
<Asp: dropdownlist id = "dropdownlist1" runat = "server" width = "100px"> </ASP: dropdownlist> </TD>
</Tr>
<Tr>
<TD style = "width: 45px; Height: 120px"> </TD>
<TD style = "width: 130px; Height: 120px; text-align: Left"> <asp: Image id = "image1" runat = "server"/> </TD>
</Tr>
</Table>
2. Background
Using system. Data. sqlclient;
Using system. IO;
Using system. drawing;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Sqlconnection con = new sqlconnection ("Server = (local); User ID = sa; Pwd =; database = db_07 ");
Sqldataadapter Ada = new sqldataadapter ("select * From tb_17", con );
Con. open ();
Dataset DS = new dataset ();
Ada. Fill (DS );
Dropdownlist1.datasource = Ds;
Dropdownlist1.datatextfield = "ID ";
Dropdownlist1.datavaluefield = "ID ";
Dropdownlist1.databind ();
Image1.visible = false;
}
}
Protected void button#click (Object sender, eventargs E)
{
Image1.visible = true;
Sqlconnection con = new sqlconnection ("Server = (local); User ID = sa; Pwd =; database = db_07 ");
String imagename = "";
Try
{
Con. open ();
Sqlcommand COM = new sqlcommand ("Select name from tb_17 where id =" + dropdownlist1.text + "", con );
Sqldatareader DR = com. executereader ();
Dr. Read ();
Memorystream MS = new memorystream (byte []) Dr ["name"]);
Bitmap image = new Bitmap (MS );
String filepath = server. mappath ("files /");
Directoryinfo dir = new directoryinfo (filepath );
Fileinfo [] filecount = dir. getfiles ();
Int I = filecount. length;
Imagename = filepath + (I + 1) + ". jpg ");
Image. Save (imagename );
Dr. Close ();
Image1.imageurl = "files/" + (I + 1) + ". jpg ");
}
Finally
{
Con. Close ();
}
}
}