Converting a File to a Base64 Encoded String
By Adam PresleySometimes it is necessary to send a file to someplace, but not practical to send it as a binary file. This is where base64 encoding comes in handy. Such a think has many uses, for example in webservices. It may be necessary to send a file via webservice to some 3rd party system. Base64 encoding allows you to overcome the firewall by converting that binary data to string data. Here’s an example of converting a file to Base64 encoding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private string ConvertFileToBase64(string Filename) { string result = ""; using (FileStream reader = new FileStream(Filename, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[reader.Length]; reader.Read(buffer, 0, (int) reader.Length); result = Convert.ToBase64String(buffer); } return result; } |
And there you have it! Send that puppy all over the internet!


[...] out my new article on encoding files as base64 strings. Useful for sending binary data across the wire where sending such binary data [...]