无法将类型为“System.String”的对象强制转换为“System.Byte []”。 [英] Unable to cast object of type 'System.String' to type 'System.Byte[]'.?
问题描述
错误是这样的...... Error is Like This .... 这篇关于无法将类型为“System.String”的对象强制转换为“System.Byte []”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<前一个=cs> 如果 (FileUploadPhoto.HasFile)
{
if (FileUploadPhoto.PostedFile.ContentLength > 0 )
{
int fl = FileUploadPhoto.PostedFile.ContentLength;
byte [] bytes = new byte < /跨度> [FL];
FileUploadPhoto.PostedFile.InputStream.Read(bytes, 0 ,fl);
ViewState [ daa] = bytes;
}
} 您正在尝试使用字符串数组预计会有字节数。这就是全部。
如果需要获取表示字符串的字节,则取决于特定的编码。此方法为您提供字节: http://msdn.microsoft.com/ en-us / library / ds4kkd55%28v = vs.110%29.aspx [ ^ ]。
如果你的字符串代表字节数,比如说,十进制或其他表示(如字符串32,12,12FFA,11),您需要解析此字符串为一些数字集合,而不是使用 ToArray
获取字节数组。怎么做,取决于字符串的内容,也很容易。
if (FileUploadPhoto.HasFile)
{
if (FileUploadPhoto.PostedFile.ContentLength > 0)
{
int fl = FileUploadPhoto.PostedFile.ContentLength;
byte[] bytes = new byte[fl];
FileUploadPhoto.PostedFile.InputStream.Read(bytes, 0, fl);
ViewState["daa"] = bytes;
}
}
You are trying to use string where array of bytes is expected, that's all.
If you need to get bytes representing string, it depends on particular encoding. This methods gives you bytes: http://msdn.microsoft.com/en-us/library/ds4kkd55%28v=vs.110%29.aspx[^].
If your string represent number of bytes in, say, decimal or other representation (like in a string "32, 12, 0xFA, 11"), you need to parse this string into some collection of numbers and than use ToArray
to get to array of bytes. How to do it, depends on content of the string and also pretty easy.