我有一个大问题。
我上传了图片并得到了 byte[]
。
在数据库中,我有 NVARCHAR(MAX)
字段并且无法更改。
无论我做什么并保存在数据库中,我以后都无法将其作为图像获取。
我试过 .ToString() 不起作用。
我试过这个:
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
当我尝试将其放入 RadBinaryImage
时,我什么也没得到。
这是错误:
The provided binary data may not be valid image or may contains unknown header
最佳答案
您可能想要 encode it , 而不是直接转换。
我过去曾成功使用 Radix-64 编码。有一个 C# 方法,但我没有使用它:
Convert.ToBase64String(byteArrayForImage);
http://msdn.microsoft.com/en-us/library/dhx0d524.aspx
然后您可以使用反向转换来获取您的字节数组:
Convert.FromBase64String(stringFromDB)
关于c# - 如何在数据库的文本字段中存储 byte[] 并检索它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14835338/