Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Save & Read Image From DatabaseUploading an Image File to a SQL databaseASP.NET provides a simple mechanism to upload image files to a database, like SQL Server. The images can be stored in a database table attribute that is of image data-type. You can use the following SQL statement to create a table that can store image files:
create table myimages In the preceding code, the table attributes are:
The images from the Web form can be loaded in the preceding database table. The uploading of image file from the web form can be categorized in the following tasks:
Creating the Web form for uploading the image filesThe HTML form that uploads the image files to a database should meet the following requirements:
You can use the following code snippet to create a form to up load the images: <form
enctype="multipart/form-data"
id="Form2"
method="post"
runat="server"> In the preceding code, the enctype attribute notifies the server that the binary data will be uploaded. The form has a file field control by the name upload_file, which you can use to browse and locate the file. The form also has two button controls, one to upload and one to view. The interface looks like as shown in the figure: Uploading Image File to a DatabaseWhen the user clicks on the Upload me button, an event handler is invoked. In the event handler you can add the following code to upload the file in the database: private
void Button1_Click(object
sender, System.EventArgs e) In the preceding code, there are three main pieces of data that are important for uploading image: Content-Type (strtype) The preceding code the file to be uploaded is sptored in img_strm, object of stream class. In img_strm the file is stored in bimary format You should convert it to byte array before uploading it into database. The following statement of the preceding code snippet shows how the file is converted into byte array: int n=img_strm.read(imgdata,0,imglen) The Stream object provides the Read() method to convert file from binary to byte array. It accepts three paramters
You can download the rest of the code form the link given at the end of the article. Retrieving an image from DatabaseThe main code performing this functionality is given below. Its the event handler for the "view image" event. The image name that is written in the text box is displayed.
private void
Button2_Click(object sender, System.EventArgs
e) You can download example Save & Retrieve Images From Database Project, used in this tutorial Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | |