Writing a Base64 String to the File System or Browser

Quite often, vendors pass images or PDFs to customers using the binary contents of the file. To make it more secure, they convert the binary contents to a Base64 string. A full explanation about Base64 encoding can be found at Wikipedia here. Anyway, when you receive the string, you cannot simply write it to a file or to the browser. First, you must convert the data into a Byte array. The sample code below demonstrates this:

Dim str As String = "Insert Your Base64 String Here"
Dim Base64Byte() As Byte = Convert.FromBase64String(str)

Then, you can decide which output method you'd prefer. If you'd like to write the content to the file system, you can write it as:

Dim obj As FileStream = File.Create("C:\test.pdf")
obj.Write(Base64Byte, 0, Base64Byte.Length)

If you'd prefer to write the content to the browser, you'd most likely add this to your page:

Response.ContentType = "application/pdf"

I'd highly recommend setting the content-disposition header attribute if you write the file to the browser. I've explained more about using this header at http://weblogs.asp.net/jgaylord/archive/2004/12/08/278309.aspx.

Published Wednesday, October 10, 2007 4:18 PM by Jason N. Gaylord


No Comments