插入图像文件到MySQL,但它插入空值...如何将图像转换为二进制以及二进制到图像 [英] inserting image file into mysql but it insert null values...how to convert image to binary as well as binary to image

查看:65
本文介绍了插入图像文件到MySQL,但它插入空值...如何将图像转换为二进制以及二进制到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像文件插入到mysql中,但它需要使用null值....如何将图像转换为二进制图像,如将二进制图像转换为图像..以在aspx页面中检索图像..

i insert image file into mysql but it takes null values....how to convert image to binary as wel as binary to image..to retrive image in aspx page..

推荐答案

尝试 [


在mysql表中,创建一个数据类型为Blob的列.然后在表单提交事件中..
Hi
In mysql table create a column with datatype Blob. Then in the form submiting event..
protected void Button1_Click(object sender, EventArgs e)
 {
     HttpPostedFile postedFile=FileUpload1.PostedFile;
     Stream stream = postedFile.InputStream;
     BinaryReader bReader=new BinaryReader(stream);
     byte[] bytes=bReader.ReadBytes((int)stream.Length);

     //STORE THIS STRING IN THE BLOB COLUMN IN MYSQL
     string blob=Convert.ToBase64String(bytes);


     // For testing--- to show the image back to browser
     byte[] bytes1 = Convert.FromBase64String(blob);
     Response.ContentType = postedFile.ContentType;
     Response.OutputStream.Write(bytes1, 0, bytes1.Count());



 }


如上所示,将发布的文件转换为Blob字符串并存储在数据库中(您可能还需要将内容类型存储在另一列中).从数据库检索,将字符串转换为字节数组.您可以将这个字节数组定向到输出流或以某种方式处理以显示在控件中.希望对您有帮助


convert your posted file to a blob string as shown above and store in the database (you may need to store the content type as well in another column). Retrive from the database , convert the string to byte array. This byte array you may direct to output stream or handle in some way to display in a control. Hope this helps


这篇关于插入图像文件到MySQL,但它插入空值...如何将图像转换为二进制以及二进制到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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