0

I have a custom object. One of the properties on the object is a byte array with the contents of a file. This file can be VERY large (800+ MB in some instances). Since using the JsonSerializer and XmlSerializer are out of the question (the resulting string is too large), I think going with a byte array is the next best option.

I've looked through some other solutions (like this) but currently have no luck with what I need. We're working out of .NET 5 and things like the BinaryFormatter are no-go.

Is it somehow possible to take my object and write it to a stream so I can deal with it in a byte array?

Thanks

Joe Morgan
  • 1,761
  • 6
  • 20
  • 29
  • You can use one of the existing formats for serialization. [`MessagePack`](https://github.com/neuecc/MessagePack-CSharp) for example or [`protobuf`](https://github.com/protobuf-net/protobuf-net) – Guru Stron Sep 21 '21 at 20:44
  • Also consider changing that property from a byte array to a Stream, so you don't have to hold it all in memory – Andrew Williamson Sep 21 '21 at 21:42
  • It would be nice to know what is the purpose of such serialization? Are you sending those files over network? Uploads a file to a DB? You may try `Response.OutputStream.Write` to [write file chunks to the response stream](https://forums.asp.net/t/2172345.aspx?Fastest+way+to+read+a+very+large+pdf+file+as+byte+array+and+view+in+in+ASP+NET+Web+Forms+4+application) (if file is being downloaded) – oleksa Sep 22 '21 at 07:43
  • @AndrewWilliamson I'd be willing to try a stream. I've been trying to figure out how to get it working with a ```StreamContent``` object or ```MemoryStream``` as well. – Joe Morgan Sep 22 '21 at 12:41
  • @oleksa I'd be sending the file across to our API for the purpose of putting in our database. We're taking video files and ultimately saving them as a byte array. – Joe Morgan Sep 22 '21 at 12:42

0 Answers0