A virtual teacher who reveals to you the great secrets of Base64

JavaScript Base64 Decode

To convert Base64 to original data, JavaScript provides the atob function which is an acronym for “ASCII to Binary”. In JavaScript Base64 values are obtained using the btoa function.

Usage:

  • atob(data) - Converts data to STRING and decodes it from Base64

Arguments:

  • data (required string) - The Base64 value you want to decode

Return Values:

  • STRING - On success, it returns the original data (it can be either text or binary)

Exceptions:

On failure the function may throw one of the following exceptions:

  • InvalidCharacterError: String contains an invalid character
  • TypeError: Not enough arguments to Window.atob

Example #1 (basic usage):

var str = atob('Z3VydQ==');
console.log(str); //-> "guru"

Example #2 (check if function is supported):

if (typeof atob !== 'function') {
  // Cannot decode Base64 values because the atob() function is not supported
  // Perhaps you want to use a polyfill or at least inform user about this
}

Example #3 (non-string data):

// Before decoding Base64 values the btoa() function converts it to String()
// It is for this reason that you will not get any errors if you pass a “wrong” data type
// For example, that is why in the example below you will get the same results
atob(null); //-> "�ée"
atob('null'); //-> "�ée"

Example #4 (unicode string):

// By default most browsers don’t support Unicode strings
// For example, this is why the btoa() function will fail to encode “€100” to Base64
// However, the atob() function doesn’t fail to decode “4oKsMTAw” (real Base64 value of the “€100”)
// In fairness, you get a completely different result, because string is treated as ASCII
var b64 = '4oKsMTAw',
  data = atob(b64);
console.log(data); //-> "�100"

For more info, check the following examples:

Comments (3)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Santosh,
    I have 2.5MB pdf base64 data URI but it is not download using given javascript code can you please provide any solution for this.
    • Administrator,
      If you have a data URI string and want just be able to download it, there is no need to decode it. You can create a HTML link that tells the browser to download the PDF file:
      <a href="data:application/pdf;base64,YOUR_B64_STRING" download="file.pdf">file.pdf</a>
  • manish96,
    I have more than 6 MB of base64 data that when I try to download it as excel file in typescript,the file is getting Repaired and throwing me that it cant be open.I have Applied the coding logic as suggested in the below metioned link.First I will convert my base64 to arraybuffer and then to arraybytes and finally the same to blob and after that download the same.This logic is working fine base64 with <6 Mb size.Kindly Help me on this.

    `https://stackoverflow.com/questions/48836541/save-binary-file-from-base64-data-in-javascript`
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.