Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert base64 string to file

Tags:

I'm trying to convert base64 strings back to the original files. The application from where I try to export these files will only allow me to export in base64 strings. This export returns the base64 string and the filetype.

How can I convert these strings back to the original files? I've been trying things like this, but I don't think this will work with different types of files?

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($file)) |
    Out-File C:\ID\document.$($extension)

Can anyone provide me some ideas on how to do this?

like image 678
Jeroen Avatar asked Feb 11 '16 09:02

Jeroen


People also ask

How do I convert Base64 to Notepad ++?

To encode or decode Base64 data you need to first highlight the entire range of data you want to be encoded or decoded. Next, click on “Plugins” in the top bar, then “MIME Tools”. In the second level of the menu you can see all of the Base64 encode and decode options.


Video Answer


1 Answers

The FromBase64String() method converts a base64-encoded string to a byte array. All you need to do is write that byte array back to a file:

$b64      = 'AAAAAA...'
$filename = 'C:\path\to\file'

$bytes = [Convert]::FromBase64String($b64)
[IO.File]::WriteAllBytes($filename, $bytes)
like image 98
Ansgar Wiechers Avatar answered Sep 19 '22 15:09

Ansgar Wiechers