Convert base64 string to file

  • Thread starter Thread starter Kliot
  • Start date Start date
K

Kliot

I have a string that is a base64 graphic that I need to convert to a
file, can someone give me some pointers on how to do this?

Thanks
 
Use the Convert.FromBase64String( ) method. This will give you the binary
data back (as a byte array).
You can then write the byte array to a file stream.

-Rob Teixeira [MVP]
 
Rob,

Thanks for the tip, I'm still very new to VB, how do I write the array to a
filestream? Do I need to add anything between the elements such as a carrige
return?

Thanks again
 
Perrin said:
Rob,

Thanks for the tip, I'm still very new to VB, how do I write the array to a
filestream? Do I need to add anything between the elements such as a carrige
return?

Very basic example :)

Imports System
Imports System.IO

Module modMain

Public Sub Main()
' get your base64 string... b64str

Dim binaryData() As Byte = Convert.FromBase64String(b64str)

' Assuming it's a jpg :)
Dim fs As New FileStream("YourImage.jpg", FileMode.CreateNew)

' write it out
fs.Write(binaryData, 0, binaryData.Length)

' close it down.
fs.Close()
End Sub

End Module

HTH
 
Tom,

Thanks for the help, that was exactly what I needed to get me going.

Perrin
 
Back
Top