Upload a photo on an ASP. NET page and save it to the database. After uploading, the uploaded photo is displayed.

Source: Internet
Author: User

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!

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.