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开发框架网发布内容,转载请附上原文出处连接
post 张国生