hai ,,,,

i am having imagefield in my page , i want to upload the image to database which is currently present in image field ,
1.i want to save the image to database in binary ...

10 Years
Discussion Span
Last Post by zAndrew

Best way to do this is to have a seperate table in your database (I will assume you're using SQL Server for this example) with the following structure:

imgId (integer data type, primary key automatically incremented)
title (varchar data type)
type (varchar data type, the MIME type of the image ie 'image/jpeg' or 'image/gif')
data (image data type)

I'll use C# to show the rest, let me know if you can't switch it to VB. You'll need to be sure you have the following namespaces declared:

using System.IO;
using System.Data;
using System.Data.SqlClient;

And then the 'meat' of the code would be this:

// initial variables
int imgSize;
string imgType;
Stream imgStream;

// get image file size
imgSize = imgUploadControl.PostedFile.ContentLength;

// get image type
imgType = imgUploadControl.PostedFile.ContentType;

// get image stream
imgStream = imgUploadControl.PostedFile.InputStream;

// additional variables
byte[] imgData = new byte[imgSize];
int readStatus;

// read the data into a byte array (for SQL storage)
readStatus = imgStream.Read(imgData, 0, imgSize);

Now you can store the imgData, imgType and a name for the image into your database. If you have any further questions, let me know.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.