无法将类型为“System.String”的对象强制转换为“System.Byte []”。 [英] Unable to cast object of type 'System.String' to type 'System.Byte[]'.?

查看:633
本文介绍了无法将类型为“System.String”的对象强制转换为“System.Byte []”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是这样的......



<前一个=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 获取字节数组。怎么做,取决于字符串的内容,也很容易。



-SA


Error is Like This ....

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.

—SA


这篇关于无法将类型为“System.String”的对象强制转换为“System.Byte []”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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