The image is saved in the SQL database in binary format and displayed on the page.

Source: Internet
Author: User

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.

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.