C#文件流Stream转数组Byte[]失败的解决方案


遇到问题

在对接华为OBS的过程中,下载文件后,直接使用常规的方法把文件流转换为byte[]会抛出异常,

Stream stream = response.OutputStream;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length)
return bytes

在获得文件流 stream.Length的时候就抛出了异常

 异常信息

System.NotSupportedException: "Specifiled method is not supported."

解决办法

由于文件流不能直接读取,需要使用另一种办法

Stream stream = response.OutputStream;
MemoryStream stmMemory = new MemoryStream();
byte[] buffer = new byte[6 * 1024];
int i;
while ((i = stream.Read(buffer, 0, buffer.Length) > 0)
{
    stmMemory.Write(buffer, 0, i);
}
byte[] arraryByte = stmMemory.ToArrary();
return arraryByte;

采用这种方案就能正确的转换stream为byte[]了

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
张国生
上一篇:C#读取被进程占用的文件,只读模式打开文件
下一篇:C#文件流Stream转数组Byte[]失败的解决方案
评论列表

发表评论

评论内容
昵称:
关联文章

PVE硬盘对应SATA口查询

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码