[英]Image to binary returning System.byte[] instead of binary format in asp.net
I'm trying to convert image to binary and then store in database. 我正在尝试将图像转换为二进制文件,然后存储在数据库中。 I have a code to do this and after several Google searches, most answers are like the code I have written.
我有执行此操作的代码,经过几次Google搜索后,大多数答案都与我编写的代码相似。 The error I have is that instead of seeing a binary format in my database I'm getting
System.byte[]
as output. 我的错误是,我没有在数据库中看到二进制格式,而是将
System.byte[]
作为输出。 I also debugged and got the same thing. 我也调试并得到了同样的东西。 Here's part of the code
这是代码的一部分
if (Upload.HasFile)
{
HttpPostedFile postedFile = Upload.PostedFile;
string filename = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(filename);
int filesize = postedFile.ContentLength;
if (fileExtension.ToLower() == ".jpg")
{
Stream stream = postedFile.InputStream;
BinaryReader binaryreader = new BinaryReader(stream);
byte[] bytes = binaryreader.ReadBytes((int)stream.Length);
Debug.WriteLine(bytes);
}
}
The result of my debug gives System.byte[]
as output. 我的调试结果将
System.byte[]
作为输出。
You can convert a byte array to string for DB storage 您可以将字节数组转换为字符串以进行数据库存储
var storedString = Convert.ToBase64String(bytes);
and get the byte array from the stored string 并从存储的字符串中获取字节数组
bytes = Convert.FromBase64String(storedString);
If you really want to use the Binary format, you can look into the SoapHexBinary class, particularly the Parse() method and Value property 如果您确实想使用Binary格式,则可以查看SoapHexBinary类,尤其是Parse()方法和Value属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.