Convert FileStream to base64 string in C#

Convert File Stream to Base64 String in C#

In this blog post, I will demonstrate how to convert a file or file stream on disk to a base64 string representation using C#. This technique is useful when you need to store or transfer binary data over media that only supports textual data. We will focus on converting a file stream to a base64 string, but keep in mind that the same technique can be used to convert base64 to a stream or stream to base64.

Why Encoding?

Encoding is necessary when you want to represent binary data using textual characters. It allows you to store or transmit binary data in a format that can be easily handled by systems that only accept text. There are various encoding methods available, but one of the most popular ones is base64 encoding/decoding.

Why Base64?

Base64 is a text representation of binary data that uses a set of 64 characters. These characters include the alphanumeric characters (both lowercase and uppercase), the “+” and “/” symbols, and the “=” padding character. Base64 is considered safe because these 64 characters can be reliably transmitted and interpreted by various systems, including legacy computers and programs.

When is Base64 Useful?

Base64 encoding is particularly useful when you need to transfer binary data as text. By converting the binary data to base64, you can represent it as a string and transmit it using text-based protocols. On the receiving side, the base64 string can be decoded back into its original binary form.

Converting File Stream to Base64 String

To convert a file stream to a base64 string in C#, you can use the following code:

public static string ToBase64String(string fileName)
{
	using (FileStream reader = new FileStream(fileName, FileMode.Open))
	{
		byte[] buffer = new byte[reader.Length];
		reader.Read(buffer, 0, (int)reader.Length);
		return Convert.ToBase64String(buffer);
	}
}

In the above code, we create a FileStream object to read the contents of the file. We initialize a byte array as a buffer to hold the file’s data. The Read method is used to read the file’s contents into the buffer. Finally, we convert the buffer to a base64 string using Convert.ToBase64String and return the result.

Converting Base64 String to File Stream

Conversely, if you have a base64 string and you want to convert it back to a file stream, you can use the following code:

public static void ToString(string fileName, string serializedFile)
{
	using (System.IO.FileStream reader = System.IO.File.Create(fileName))
	{
		byte[] buffer = Convert.FromBase64String(serializedFile);
		reader.Write(buffer, 0, buffer.Length);
	}

}

In the code above, we create a FileStream object to write the data to a file. We convert the base64 string to a byte array using Convert.FromBase64String. Then, we use the Write method to write the byte array to the file.

Please note that the code provided is for demonstration purposes and may require additional error handling and validation when used in production scenarios.

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post