J
John Smith
I know that uploading an image to a database has been covered, oh, about 3
trillion times. However, I haven't found anything covering uploading to a
MySQL database with .net. Please don't recommend storing the image to the
filesystem and only keeping a pointer to that in the table. I want to dump
the image to a table. My code dumps the data into the table, however, I get
the following error when trying to view the image "the image ... cannot be
displayed because it contains errors".
The image data is stored in a large-blob field, extension is stored in
varchar, and a description is stored in a varchar. I think the problem
has to do with special characters in the image data. MySQL seems to be real
picky about that stuff. Looking at
http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If
you want to insert binary data into a string column (such as a BLOB), the
following characters must be represented by escape sequences: " towards the
bottom.
So, I guess I need to somehow parse the inputstream and/or byte array and
replace those characters as needed. This is where I'm stuck. I could write
a function to replace the data with the proper escape sequence but each byte
in the arrFile array contains an integer value as shown with the loop:
***********
for (int i=0; i<=intFileLen-1; i++)
Response.Write(arrFile);
***********
Is there anyway to view the raw data or is that the raw data? Any ideas?
Thanks in advance. The code follows
// Determine File Type
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(fileLen-4,4);
// Grab the contents of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength;
byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();
// Add Uploaded file to database
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrFile+"\" )";
cmdInsert = new OdbcCommand( strInsert, conMyData );
conMyData.Open();
cmdInsert.ExecuteNonQuery();
conMyData.Close();
}
trillion times. However, I haven't found anything covering uploading to a
MySQL database with .net. Please don't recommend storing the image to the
filesystem and only keeping a pointer to that in the table. I want to dump
the image to a table. My code dumps the data into the table, however, I get
the following error when trying to view the image "the image ... cannot be
displayed because it contains errors".
The image data is stored in a large-blob field, extension is stored in
varchar, and a description is stored in a varchar. I think the problem
has to do with special characters in the image data. MySQL seems to be real
picky about that stuff. Looking at
http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If
you want to insert binary data into a string column (such as a BLOB), the
following characters must be represented by escape sequences: " towards the
bottom.
So, I guess I need to somehow parse the inputstream and/or byte array and
replace those characters as needed. This is where I'm stuck. I could write
a function to replace the data with the proper escape sequence but each byte
in the arrFile array contains an integer value as shown with the loop:
***********
for (int i=0; i<=intFileLen-1; i++)
Response.Write(arrFile);
***********
Is there anyway to view the raw data or is that the raw data? Any ideas?
Thanks in advance. The code follows
// Determine File Type
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(fileLen-4,4);
// Grab the contents of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength;
byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();
// Add Uploaded file to database
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrFile+"\" )";
cmdInsert = new OdbcCommand( strInsert, conMyData );
conMyData.Open();
cmdInsert.ExecuteNonQuery();
conMyData.Close();
}