Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
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)
{
# Region test write
If (! Fileupload1.hasfile)
{
Response. Write ("select image ");
}
String fileex = system. Io. Path. getextension (this. fileupload1.filename); // get the image Extension
If (fileex! = ". GIF" & fileex! = ". Jpg" & fileex! = ". Jpeg" & fileex! = ". BMP ")
{
Response. Write ("only images can be uploaded ");
}
Else
{
// Imagetype is useful. It is required for reading images.
Int imgsize = This. fileupload1.postedfile. contentlength; // The image size in bytes)
String imgtype = This. fileupload1.postedfile. contenttype; // image type
String imgpath = This. fileupload1.postedfile. filename; // obtain the image path.
String imgname = This. fileupload1.filename; // obtain the image name
Int imglength = This. fileupload1.filename. length; // obtain the image length.
If (imglength <= 0) // determines whether an image exists.
Return;
Byte [] imgbyte = new byte [imgsize]; // use the image length to initialize a byte array to store temporary image files
Stream stream = This. fileupload1.postedfile. inputstream; // create a file stream object
Stream. Read (imgbyte, 0, imgsize); // read the image data to the temporary storage body imgbyte. 0 indicates the Data Pointer position, and filelength indicates the Data Length.
// This. fileupload1.filebytes
Try
{
String connection = configurationmanager. connectionstrings ["dbconn"]. connectionstring;
Sqlconnection sqlconn = new sqlconnection (connection );
Sqlconn. open ();
String sqlcmd = "insert into image values (@ imagetype, @ imagedata, @ imagetitle )";
Sqlcommand SQC = new sqlcommand (sqlcmd, sqlconn );
SQC. Parameters. Add ("@ imagetype", sqldbtype. varchar, 50). value = imgtype;
SQC. Parameters. Add ("@ imagedata", sqldbtype. Image, imgsize). value = This. fileupload1.filebytes; // imgbyte; // assign a binary image to @ imagedata
SQC. Parameters. Add ("@ imagetitle", sqldbtype. varchar, 50). value = imgname;
Int result = SQC. executenonquery ();
Sqlconn. Close ();
If (result! = 0)
{
Response. Write ("OK ");
}
Else
{
Response. Write ("no ");
}
}
Catch (exception ex)
{
Response. Write (ex. stacktrace );
}
Finally
{
}
}
# Endregion
}
}
This is stored.
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. Data. sqlclient;
Public partial class default2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
# Region test reading
String sqlcmd = "select top 1 * from image order by imageid DESC ";
String connection = configurationmanager. connectionstrings ["dbconn"]. connectionstring;
Sqlconnection sqlconn = new sqlconnection (connection );
Try
{
Sqlconn. open ();
Sqlcommand SQC = new sqlcommand (sqlcmd, sqlconn );
// Response. contenttype = "image/pjpeg ";
Using (sqldatareader SDR = SQC. executereader ())
{
While (SDR. Read ())
{
// Response. contenttype = SDR ["imagetype"]. tostring ();
Response. Clear ();
Response. binarywrite (byte []) SDR ["imagedata"]);
}
}
// SDR. Close ();
// Sqlconn. Close ();
}
Catch (exception ex)
{
Response. Write (ex. stacktrace );
}
Finally
{
Sqlconn. Close ();
}
# Endregion
}
}
}
This is obtained. You need to put a container on the page to receive images!