473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

ASP.NET mySQL BLOB

Hello --

I'm having a heck of a time grabbing a blob ( a jpeg image) from a
mySQL database and displaying it on a page.

I am able to connect to the database and retrieve the data, however
when the page loads, it just spews the binary garbage rather than
displaying the image.

Here's the code:

string _connectionString = ConfigurationManager.ConnectionStrings[ "DB"
].ToString(); string imageQuery = "SELECT fullsize AS image_data
FROM table WHERE id ='A1'";

OdbcConnection connection = new OdbcConnection(
ConfigurationManager,ConnectionStrings["DB"].ToString() );

OdbcCommand cmd = new OdbcCommand( imageQuery, connection );

DataSet dsImage = new DataSet();

Response.ContentType = "image/jpeg";
Response.BinaryWrite( ( Byte[] )dsImage.Tables[ 0 ].Rows[ 0 ][
"image_data" ] );

It appears to be a problem with Base64Decoding... but I'm stumped. I'm
basically trying to replicate some PHP code:
<?

$image_data = (isset($_GET['swatchthumb'])) ? 'select swatchthumb as
image_data from birdie_product_model where uid = "'.$_GET['uid'].'"' :
'select fullsize as image_data from birdie_product_model where uid =
"'.$_GET['uid'].'"';

$image_data = mysql_fetch_object(mysql_query($image_data));

header("Content-type: image/jpeg");

echo base64_decode($image_data->image_data);
?>
Any insight (beyond the obvious (don't use Blobs!) :-) ) would be MOST
appreciated!

Regards,

Cuyler Jones

Aug 27 '06 #1
4 3772
KJ
I believe you need a little more in the headers. For example (code not
tested):

Response.Clear();
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Length", ( Byte[] )dsImage.Tables[ 0
].Rows[ 0 ]["image_data" ].Length.ToString());
Response.AppendHeader("Content-Disposition","inline;filename=AName.jpeg);
Response.BinaryWrite(( Byte[] )dsImage.Tables[ 0 ].Rows[ 0
]["image_data" ]);
Response.End();

cu**********@gmail.com wrote:
Hello --

I'm having a heck of a time grabbing a blob ( a jpeg image) from a
mySQL database and displaying it on a page.

I am able to connect to the database and retrieve the data, however
when the page loads, it just spews the binary garbage rather than
displaying the image.

Here's the code:

string _connectionString = ConfigurationManager.ConnectionStrings[ "DB"
].ToString(); string imageQuery = "SELECT fullsize AS image_data
FROM table WHERE id ='A1'";

OdbcConnection connection = new OdbcConnection(
ConfigurationManager,ConnectionStrings["DB"].ToString() );

OdbcCommand cmd = new OdbcCommand( imageQuery, connection );

DataSet dsImage = new DataSet();

Response.ContentType = "image/jpeg";
Response.BinaryWrite( ( Byte[] )dsImage.Tables[ 0 ].Rows[ 0 ][
"image_data" ] );

It appears to be a problem with Base64Decoding... but I'm stumped. I'm
basically trying to replicate some PHP code:
<?

$image_data = (isset($_GET['swatchthumb'])) ? 'select swatchthumb as
image_data from birdie_product_model where uid = "'.$_GET['uid'].'"' :
'select fullsize as image_data from birdie_product_model where uid =
"'.$_GET['uid'].'"';

$image_data = mysql_fetch_object(mysql_query($image_data));

header("Content-type: image/jpeg");

echo base64_decode($image_data->image_data);
?>
Any insight (beyond the obvious (don't use Blobs!) :-) ) would be MOST
appreciated!

Regards,

Cuyler Jones
Aug 28 '06 #2
Thank you for the information. Unfortunately it has yeilded the same
results.

Regards,

Cuyler Jones
KJ wrote:
I believe you need a little more in the headers. For example (code not
tested):

Response.Clear();
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Length", ( Byte[] )dsImage.Tables[ 0
].Rows[ 0 ]["image_data" ].Length.ToString());
Response.AppendHeader("Content-Disposition","inline;filename=AName.jpeg);
Response.BinaryWrite(( Byte[] )dsImage.Tables[ 0 ].Rows[ 0
]["image_data" ]);
Response.End();

cu**********@gmail.com wrote:
Hello --

I'm having a heck of a time grabbing a blob ( a jpeg image) from a
mySQL database and displaying it on a page.

I am able to connect to the database and retrieve the data, however
when the page loads, it just spews the binary garbage rather than
displaying the image.

Here's the code:

string _connectionString = ConfigurationManager.ConnectionStrings[ "DB"
].ToString(); string imageQuery = "SELECT fullsize AS image_data
FROM table WHERE id ='A1'";

OdbcConnection connection = new OdbcConnection(
ConfigurationManager,ConnectionStrings["DB"].ToString() );

OdbcCommand cmd = new OdbcCommand( imageQuery, connection );

DataSet dsImage = new DataSet();

Response.ContentType = "image/jpeg";
Response.BinaryWrite( ( Byte[] )dsImage.Tables[ 0 ].Rows[ 0 ][
"image_data" ] );

It appears to be a problem with Base64Decoding... but I'm stumped. I'm
basically trying to replicate some PHP code:
<?

$image_data = (isset($_GET['swatchthumb'])) ? 'select swatchthumb as
image_data from birdie_product_model where uid = "'.$_GET['uid'].'"' :
'select fullsize as image_data from birdie_product_model where uid =
"'.$_GET['uid'].'"';

$image_data = mysql_fetch_object(mysql_query($image_data));

header("Content-type: image/jpeg");

echo base64_decode($image_data->image_data);
?>
Any insight (beyond the obvious (don't use Blobs!) :-) ) would be MOST
appreciated!

Regards,

Cuyler Jones
Aug 28 '06 #3
Any insight (beyond the obvious (don't use Blobs!) :-) ) would be MOST
appreciated!
First off then, you might want to use the mysql connector.
Which is bound to perform a lot better then ODBC

This is a help segment talking about blobs:

http://dev.mysql.com/doc/refman/5.0/...sing-blob.html

Release version:
http://dev.mysql.com/downloads/connector/net/1.0.html
Aug 28 '06 #4
Mischa,

Thank you for your input. I tried the mySQL connector and followed the
example, however I ended up in the same spot.

The line: FileSize = myData.GetUInt32(myData.GetOrdinal("file_size"));
from the example, throws and exception. (Cannot convert type
System.Byte[] to System.IConvertable).

One of the problems is that I do not have the size of the file stored
in the database, so I had to get it manually:

string test = myData.GetString(0);
FileSize = Convert.ToUInt32( test.Length );
....

Addtionally, the line: myData.GetBytes(myData.GetOrdinal("file"), 0,
rawData, 0, FileSize); from the example requires a cast of "FileSize"
from UInt32 to int, which is a potential issue.

I am beginning to think that what I am trying to do is impossible with
the C#2.0 / mySQL combination, or the problem is beyond my current
skill level, as I have exhausted every resource that I can think of.

Regards,

Cuyler Jones
Mischa Kroon wrote:
Any insight (beyond the obvious (don't use Blobs!) :-) ) would be MOST
appreciated!

First off then, you might want to use the mysql connector.
Which is bound to perform a lot better then ODBC

This is a help segment talking about blobs:

http://dev.mysql.com/doc/refman/5.0/...sing-blob.html

Release version:
http://dev.mysql.com/downloads/connector/net/1.0.html
Aug 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sugapablo | last post by:
Is there any PHP function that will allow me to determine the MIME type of a blob stored in MySQL? Specifically, if I'm storing an image as a blob in MySQL, is there any PHP function that can...
1
by: Kirby Urner | last post by:
I've been testing the Cookbook example 8.6 (2002 edition) re using cPickle to insert and retrieve BLOBs from mySQL, using Python's MySQLdb module. When I try to cPickle.loads(blob), I get an...
0
by: Dyego Souza do Carmo | last post by:
Hi, I'd found a bug when I'd tried to use blob fields and innoDb. To see the bug, do the follow: *) Create a database. *) Import the dump file that is attached to this e-mail....
7
by: sime | last post by:
Hi, I have a blob field in a mysql database table. I want to copy a blob from one record to another. I am having trouble transferring the data via a php variable. Maybe I need to addslashes or...
2
by: pmz | last post by:
Dear Group, I'm connecting in C# with remote (BSD) MySQL server with ODBC Driver, and I'm trying to find the best sollution in such problem: As I've read on MySQL manual, they have suggested...
7
by: greywire | last post by:
So I need to load lots of data into my database. So I discover LOAD DATA INFILE. Great! This little gem loads my CSV in blazing times (compared to parsing the file and doing INSERT for each...
2
by: Vinciz | last post by:
hi guys... im new in java and i would love to learn some of these... basically i got a sample code to retrieve the blob from the mysql. however, i dont really know what to do with these...
0
by: dimitri pater | last post by:
Hi, I am trying to insert an image, which is stored as a blob in MySQL, into a table using Reportlab. I have tried this: from StringIO import StringIO cfoto = StringIO(result) # where result...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
6
by: Keith Hughitt | last post by:
Hi all, I've run into a strange error while trying to store some PNG images in a MySQL database using MySQLdb. When I try to insert smaller images (< 64kb or so) everything seems to work fine....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.