Convert mp4 file to byte array and string

531 Views Asked by At

I working on project to upload files into firebase real-time database using firesharp in WinForm c#.

I searched a lot to understand how to do this.

I know about the option to write File.ReadAllBytes(), but I want to run the app on weak PC, with 4 GB ram, and he is very slow. I succeeded to upload an image, it's work good. I find something about Stram option, but I don't know how to do it with converting to String.

Sorry about my English.

1

There are 1 best solutions below

4
Hao Yu-MSFT On

You can convert the file to binary, and the binary can be saved as arrays and strings.

//Read file to byte array

FileStream stream = File.OpenRead(@"c:\path\to\your\file\here.txt");
byte[] fileBytes= new byte[stream.Length];

stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
//Begins the process of writing the byte array back to a file

using (Stream file = File.OpenWrite(@"c:\path\to\your\file\here.txt"))
{
   file.Write(fileBytes, 0, fileBytes.Length);
}

https://www.codeproject.com/Questions/636646/Csharp-file-to-Byte-Array-and-Byte-Array-to-File

FileStream st = new FileStream(@"C:\2.jpg", FileMode.Open); 
            byte[] buffer = new byte[st.Length]; 
            st.Read(buffer, 0, (int)st.Length); 
            st.Close(); 
            Console.ReadLine(); 

https://social.msdn.microsoft.com/Forums/en-US/648bd8b2-74ec-4054-9c68-68190b600971/how-to-convert-a-file-to-binary?forum=csharplanguage