将图像上传到mysql表 [英] uploading image to mysql table

查看:82
本文介绍了将图像上传到mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道将图像上传到数据库已经被覆盖了,哦,大约3亿美元。但是,我还没有找到任何涉及使用.net上传到

MySQL数据库的内容。请不要建议将图​​像存储到

文件系统中,只保留指向表中的图像。我想将图像转储到表格中。我的代码将数据转储到表中,但是,当我试图查看图像时,我得到了以下错误:图像...不能显示为
因为它包含错误。


图像数据存储在一个大blob字段中,扩展名存储在
varchar中,描述存储在varchar中。我认为问题

与图像数据中的特殊字符有关。 MySQL似乎是真实的

挑剔那些东西。查看
http://dev.mysql。 com / doc / mysql / en / String_syntax.html ,有一个模糊的如果你希望将二进制数据插入到字符串列(例如BLOB)中,则跟随字符的

必须由转义序列表示:"朝向

底部。


所以,我想我需要以某种方式解析输入流和/或字节数组并且

替换这些字符根据需要。这就是我被困住的地方。我可以编写

a函数来用适当的转义序列替换数据但是arrFile数组中的每个字节

都包含一个整数值,如循环所示:


***********

for(int i = 0; i< = intFileLen-1; i ++)

Response.Write(arrFile [i]);

***********


无论如何都要查看原始数据还是原始数据?有什么想法吗?

提前致谢代码如下

//确定文件类型

int fileLen = txtFileContents.PostedFile.FileName.Length;

strFileExtension =

txtFileContents.PostedFile.FileName.Substring(文件Len-4,4);


//抓取上传文件的内容

intFileLen = txtFileContents .PostedFile.ContentLength;


byte [] arrFile = new byte [intFileLen];

objStream = txtFileContents.PostedFile.InputStream;

objStream.Read(arrFile,0,intFileLen);

objStream.Close();


//将上传的文件添加到数据库

strInsert =" Insert into pics(TITLE,IMAGE_TYPE,IMAGE)值(\"" +

txtFileTitle.Text +" \",\" " + strFileType +" \",\"" + arrFile +" \")" ;;


cmdInsert = new OdbcCommand(strInsert, conMyData);


conMyData.Open();

cmdInsert.ExecuteNonQuery ();


conMyData.Close();

}

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[i]);
***********

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(file Len-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();
}

推荐答案



我还没有这样做,但正如我所见,你正在使用ODBC驱动程序来支持
MySQL。您是否也尝试过他们的ADO.Net提供商?当我看起来通过

代码时,它支持二进制blob,所以也许这是要走的路。


Sunny

In文章< uy ************** @ TK2MSFTNGP09.phx.gbl>,
没有********* @ hotmail.com 说...
我知道将图像上传到数据库已被覆盖,哦,大约3
万亿次。但是,我还没有找到任何涉及使用.net上传到MySQL数据库的内容。请不要建议将图​​像存储到
文件系统中,只保留指向表中的图像。我想将图像转储到表格中。我的代码将数据转储到表中,但是,当我尝试查看图像时,我得到以下错误图像...因为包含错误而无法显示...

图像数据存储在大blob字段中,扩展名存储在
varchar中,描述存储在varchar中。我认为问题
与图像数据中的特殊字符有关。 MySQL似乎真的很挑剔。查看
http://dev.mysql。 com / doc / mysql / en / String_syntax.html ,有一个模糊的如果您想将二进制数据插入字符串列(例如BLOB),则后续字符必须由转义序列表示:"朝着
底部。

所以,我想我需要以某种方式解析输入流和/或字节数组,并根据需要替换这些字符。这就是我被困住的地方。我可以编写一个函数用适当的转义序列替换数据,但是arrFile数组中的每个字节都包含一个整数值,如循环所示:

*** ********
for(int i = 0; i< = intFileLen-1; i ++)
Response.Write(arrFile [i]);
**** *******

无论如何都要查看原始数据还是原始数据?有什么想法吗?
提前致谢。代码如下

//确定文件类型
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(file Len-4,4);

//抓取上传文件的内容
intFileLen = txtFileContents.PostedFile.ContentLength;

byte [] arrFile = new byte [intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read(arrFile,0,intFileLen);
objStream.Close();

/ /将上传的文件添加到数据库
strInsert ="插入图片(TITLE,IMAGE_TYPE,IMAGE)值(\"" +
txtFileTitle.Text +" \",\ "" + strFileType +" \",\"" + arrFile +" \")" ;;

cmdInsert = new OdbcCommand(strInsert,conMyData) ;

conMyData.Open();
cmdInsert.ExecuteNonQuery();

conMyData.C丢失();
}
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[i]);
***********

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(file Len-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();
}



Sunny--


谢谢你答复。你在谈论ByteFX提供商吗?不,我没有尝试过,但我刚刚做过。同样的事情。


我确实设法弄清楚如何用转义序列替换数据中的特殊字符......我想。虽然,它仍然没有奏效。我做了什么

将图像数据字节数组转换为ascii编码字符串,使用

替换方法用转义序列替换特殊字符然后

将其转换回字节数组。就像我说的那样,它仍然没有工作。代码如下:


// txtFileContents是文本上传控件

//抓取内容并放入字节数组

intFileLen = txtFileContents.PostedFile.ContentLength;

byte [] arrFile = new byte [intFileLen];

objStream = txtFileContents.PostedFile.InputStream;

objStream.Read(arrFile,0,intFileLen);

objStream.Close();


//现在将其转换为字符串并根据需要处理转义序列

编码encEncoder = ASCIIEncoding.ASCII;

string str = encEncoder.GetString(arrFile,0,arrFile.GetLength(0));

string str2 =

str.Replace(" \""," \\\"")。替换('''',','\\ \\\''")。替换(\\,\\\\)。替换(((c / /
har) 0).ToString()," \\\\");


//现在转换回字节数组

byte [] arrCr ap = unicode.GetBytes(str2);


//插入表格...省略了ado连接的东西

strInsert =" Insert into pics (TITLE,IMAGE_TYPE,IMAGE)值(\"" +

txtFileTitle.Text +" \",\"" + strFileType +" \" ,\"" + arrCrap +" \" )" ;;


//结束

/////////////////////// ///////


Sunny <苏****** @ icebergwireless.com>在消息中写道

新闻:O5 ************** @ TK2MSFTNGP09.phx.gbl ...
Sunny--

Thanks for your reply. Are you talking about the ByteFX provider? No I
hadn''t tried that but I just did. Same thing.

I did manage to figure out how to replace special characters in the data
with escape sequences...I think. Though, it still doesn''t work. What I did
was convert the image data byte array into an ascii encoded string, used the
Replace method to replace the special chars with escape sequences and then
converted that back to a byte array. Like I said, though, it still didn''t
work. Code is below:

// txtFileContents is the text upload control
//grab contents and put into byte array
intFileLen = txtFileContents.PostedFile.ContentLength;
byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();

// now convert that to string and process escape sequences as needed
Encoding encEncoder = ASCIIEncoding.ASCII;
string str = encEncoder.GetString(arrFile,0,arrFile.GetLength(0 ));
string str2 =
str.Replace("\"","\\\"").Replace("''","\\''").Replac e("\\","\\\\").Replace(((c
har)0).ToString(),"\\0");

// now convert back to byte array
byte[] arrCrap = unicode.GetBytes(str2);

// insert into table...left out the ado connection stuff
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

// end
//////////////////////////////

"Sunny" <su******@icebergwireless.com> wrote in message
news:O5**************@TK2MSFTNGP09.phx.gbl...
我还没有这样做,但正如我所看到的,你正在使用ODBC驱动程序来实现MySQL。您是否也尝试过他们的ADO.Net提供商?当我看起来通过
代码时,它支持二进制blob,所以也许这是要走的路。

Sunny

文章< uy **** **********@TK2MSFTNGP09.phx.gbl>,
没有**** *****@hotmail.com 说...
我知道将图像上传到数据库已经被覆盖了,哦,大约3亿美元左右。但是,我还没有发现任何涉及使用.net上传到
MySQL数据库的内容。请不要建议将图​​像存储到
文件系统中,只保留指向表中的图像。我想
将图像转储到表格中。我的代码将数据转储到表中,但是,I
在尝试查看图像时出现以下错误图像...因为它包含错误而无法显示


图像数据存储在大blob字段中,扩展名存储在
varchar中,描述存储在varchar中。我认为
问题与图像数据中的特殊字符有关。对于那些东西,MySQL似乎是真正挑剔的。查看
http://dev.mysql。 com / doc / mysql / en / String_syntax.html ,有一个模糊的
如果要将二进制数据插入字符串列(例如BLOB),
必须用转义序列表示以下字符:朝向
底部。

所以,我想我需要以某种方式解析输入流和/或字节数组
并根据需要替换这些字符。这就是我被困住的地方。我可以用
编写一个函数来用适当的转义序列替换数据,但是arrFile数组中的每个
字节都包含一个整数值,如循环所示:

*** ********
for(int i = 0; i< = intFileLen-1; i ++)
Response.Write(arrFile [i]);
**** *******

无论如何都要查看原始数据还是原始数据?任何
的想法?提前致谢。代码如下

//确定文件类型
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(file Len-4,4);

//抓取上传文件的内容
intFileLen = txtFileContents.PostedFile.ContentLength;

byte [] arrFile = new byte [intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read(arrFile,0,intFileLen);
objStream.Close();

/ /将上传的文件添加到数据库
strInsert ="插入图片(TITLE,IMAGE_TYPE,IMAGE)值
(\"" + txtFileTitle.Text +" \",\ "" + strFileType +" \",\"" + arrFile +" \")" ;;

cmdInsert = new OdbcCommand(strInsert,conMyData) ;

conMyData.Open();
cmdInsert.ExecuteNonQuery();

conMyData.Cl ose();
}
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[i]);
***********

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(file Len-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();
}



嗨John,


在文章< uC ************** @ TK2MSFTNGP09.phx.gbl>中,
没有********* @ hotmail.com 说...
Hi John,

In article <uC**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
Sunny--

感谢您的回复。你在谈论ByteFX提供商吗?不,我没试过,但我刚刚做过。一样。


是的,这是我的想法。

//现在转换回字节数组
byte [] arrCrap = unicode.GetBytes(str2) ;

//插入表格...省略了ado连接内容
strInsert ="插入图片(TITLE,IMAGE_TYPE,IMAGE)值(\"" +
txtFileTitle.Text +" \",\"" + strFileType +" \",\"" + arrCrap +" \")" ;;
Sunny--

Thanks for your reply. Are you talking about the ByteFX provider? No I
hadn''t tried that but I just did. Same thing.
Yes, that was my idea.
// now convert back to byte array
byte[] arrCrap = unicode.GetBytes(str2);

// insert into table...left out the ado connection stuff
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";



这是如何工作的???如何添加byte []字符串的一部分

连接?最后一行是否可以编译?


Sunny


And how this works??? How can you add byte[] a part of string
concatenation? Does the last line compile at all?

Sunny


这篇关于将图像上传到mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆