We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 96,437 times

Contents

Related Categories

Uploading Images to a Database - The Good Stuff

dave123aspx

The Good Stuff

Ok, we know how to connect to the database, we know how to insert data into the database, and we have access to the uploaded image's properties. But how do we pass the stream of the image to SaveToDB(). Again, .NET comes to the rescue. With 1 line of code we are able to access the image stream and convert it to a Byte array.

int n = imgStream.Read(imgBinaryData,0,imgLen);

The stream object provides a method called Read(). Read() takes 3 parameters:

  • buffer - An array of bytes. A maximum of count bytes are read from the current stream and stored in buffer.
  • offset - The byte offset in buffer at which to begin storing the data read from the current stream.
  • count - The maximum number of bytes to be read from the current stream.

So we pass in our Byte array, imgBinaryData; the place to start at, 0; and the amount of bytes we want to read. n number of bytes read into our array is returned.

Extending beyond images

Because we are able to access the binary stream of data, images are not the only object we can store in the database. Some other objects might be streaming video, com objects, or sound clips.

Conclusion

So there we have it, ASP.NET provides us some easy functionality for uploading images into a database. In Part II, we will actually look at pulling these images out of a database and sending them to a browser. The complete code used for this article can be found on the next page.

Comments

  • Re: [3905] Uploading Images to a Database

    Posted by KingNetSurfer on 15 Jun 2006

    This may sound weird but I think I'm the only one not having perfect success with this.

    I'm getting a System.NullReferenceExc...

  • Image upload and thumbnails

    Posted by zioturo on 02 Nov 2005

    The easiest way to upload and resize an image to the internet and automatically create thumbnails is I-Load.
    I-Load is a FREE ASP.NET web control with numerous benefits and features.
    You can downloa...

  • Uploading

    Posted by harshguglani on 03 Sep 2005

    Hi, Whts the benifit of uploading images in database server when i can place images on web server in my directory and accesss them. Reply soon.

  • enctype?

    Posted by LesBillBell on 02 Aug 2005

    The explanatory text says "The first interesting point about our webform, is the attribute "enctype". Enctype tells the browser and server that we will be uploading some type of binary data" but I don...

  • Posted by LinuxGold on 08 Jul 2005

    Better control of file updates. On file server, anybody can update files without letting anyone know about it. If you store a file inside SQL database, it will show who last updated files, inform us...