There are only two ways to store images. One is to create an image folder and directly read the image. The other is to put the image in the database and then read the image from the database. For me, if there are not many images, I like to put them into the database, which is conducive to project maintenance.
1. store images in the database in binary format
First, add the upload control to the page:
Code
< TD Class = "Label" >
Upload photo:
</ TD >
< TD Class = "Data" >
< ASP: fileupload ID = "Fileuploadphoto" Runat = "Server" />
< Span Class = "Validater" >
< ASP: regularexpressionvalidator ID = "Regularexpressionvalidator1" Runat = "Server" Errormessage = "Only image (.gif, .jpg) files are supported. "
Controltovalidate = "Fileuploadphoto" Display = "NONE" Validationexpression = ". * (\. GIF) | (\. jpg) $" > </ ASP: regularexpressionvalidator >
< Ajaxtoolkit: validatorcalloutextender ID = "Regularexpressionvalidator1_validatorcalloutextender"
Runat = "Server" Enabled = "True" Highlightcssclass = "Validateerror" Targetcontrolid = "Regularexpressionvalidator1" >
</ Ajaxtoolkit: validatorcalloutextender >
</ Span >
</ TD >
BackgroundCodeJust one sentence:
Expert. Image = fileuploadphoto. filebytes; Save the database and save the image to the database.
2. Read images from the database:
Create a new page named showimage. aspx; as follows:
The HTML of the page is
Code
<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Showimage. aspx. CS " Inherits = " Srims. Website. showimage " %>
The background code is:
Code
Private Const String Filetype = " Image/jpg " ;
Protected Void Page_load ( Object Sender, eventargs E)
{
VaR expert = Databasegateway
. Getnewdatabase ()
. Experts
. Getbyid (convert. toint32 (request [ " Expertid " ]);
response. buffer = true ;< br> response. clear ();
response. contenttype = filetype;
response. outputstream. write (expert. image, 0 , expert. image. length);
Response. Flush ();
Response. End ();
}
Then add the image control to the page where you want to display the image and point the image URL of the control to this page:
Code
Imageexpert. imageurl= String. Format ("Showimage. aspx? Expertid = {0}", Request ["Expertid"]);
This completes the function of accessing images in the database.