Library tutorials & articles
Uploading Images to a Database
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.
Related articles
Related discussion
-
update database when the website is nor running on the browser
by hepsy.i (1 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
Need help for web application
by bnitinn (1 replies)
-
Index out of Range
by alexhiggins732 (7 replies)
-
index out of range
by diwa.net (2 replies)
Related podcasts
-
2008 Year in Review
CodeCast Episode 9: 2008 Year in ReviewOur special 2008 year in review episode with hosts Ken Levy and Markus Egger, joined by special guest co-host Rick Strahl of West Wind Technologies. Topics · Visual Studio (@ 2:37) · Languages (@ 6:28) · SQL Server (@ 10:15) · ...
Events coming up
-
Nov
19
SQLBits V
Newport, United Kingdom
SQLBits is Europe's largest SQL Server conference, and SQLBits V will be the biggest and best yet. On November 19th we are holding a day of pre-conference seminars; on November 20th we have a pay-to-attend day of SQL Server 2008 and R2 content; and on Saturday November 21st we have our usual free community conference.
This may sound weird but I think I'm the only one not having perfect success with this.
I'm getting a System.NullReferenceException: Object reference not set to an instance of an object.
on the line
I'm not sure why I'm getting this error. I copied the code verbatum and then added references for my other fields that I'm pulling in.
I have no idea what the stack trace is all about, like I understand stacks, but the way these are formatted makes no sense to me, although here's what's in it maybe you guys will know.
((my button is exactly the same just named BtnUpload as opposed to UploadBtn.))
If anyone could help it would be greatly appreciated. It's been driving me nuts I've run into a bunch of other errors that I've fixed, mostly missing a ";" but I've taken care of them all, this is the only one I haven't figured out yet (well unless there's more after this one is fixed)
Thanks in advance for the help
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 download I-Load (it's FREE!) and view an online demo here:
http://www.radactive.com/en/Products/ILoad/Overview.aspx
Les
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 users when it expires, requiring updates and so on. I administer 230 manual documents that shows original creator, date of implementation, maintainer and updated date. Very useful and uniform in updates and maintenance.
i have been able to implement this and i now upload all sorts of documents including images into my sql server database. i now need to retrieve the document in the original format (.doc, .xls, .jpg, etc). pls, i do i achieve that?
Hello,
I need some more idea on this topic. What is benifit to store data into SQL server (means into BLOB chartype in SQL). Why can not we just store file on to PC and just add link name into SQL.
Please can anybody give me this ans.
Thanks
Ok,
Im sorta new to ASP.Net and Ive got the image upload to blob working ...
But for this to be moreso practical, it would be nice to see the ability to upload into another field, the thumbnail automatically generated from the blob data. (or when it gets uploaded).
Can someone help or point me in the right direction?!?!
Im desperate
This thread is for discussions of Uploading Images to a Database.