C#_Byte Array轉換成FileStream存出檔案
首先將接收到的
byte array寫入至MemoryStream
再藉由MemoryStream寫入至FileStream(在初始時順帶指定存入至硬碟路徑)
最後記得沒用的Stream都要關閉以及釋放
byte[] fileData = restRes.RawBytes;
MemoryStream ms = new MemoryStream();
ms.Write(fileData, 0, fileData.Length);
ms.Seek(0, SeekOrigin.Begin);
FileStream fileStream = new FileStream(CurDProjectDir + @"\TestData\test.pdf", FileMode.Create);
ms.WriteTo(fileStream);
fileStream.Close();
fileStream.Dispose();
ms.Close();
ms.Dispose();
Ref:
Save and load MemoryStream to/from a file
https://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file
Convert a Byte Array to a Stream in C#
https://medium.com/@stevenscript/convert-a-byte-array-to-a-stream-in-c-840889a59174
How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?
https://stackoverflow.com/questions/4736155/how-do-i-convert-struct-system-byte-byte-to-a-system-io-stream-object-in-c/4736185
留言
張貼留言