Software⏱️ 3 min read📅 2026-06-03

How to Fix: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

Base64 encoding issue in Google Chrome due to non-Latin1 characters in XML string.

Quick Answer: Use a different encoding method or escape special characters before base64 encoding.

The error 'Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range' is thrown only in Google Chrome, according to your tests. This issue occurs when you're base64 encoding a large XML file so that it can be downloaded.

This error is actually quite frustrating because normally, Google Chrome does not exhibit this behavior.

🔍 Why This Happens

  • The main reason for this error is that the `btoa()` function in Google Chrome cannot handle characters outside of the Latin1 range. This is a known limitation of the `btoa()` function in older versions of Chrome.
  • Another possible cause could be the presence of non-ASCII characters in the XML file being encoded.

🛠️ Step-by-Step Verified Fixes

Base64 Encoding using ASCII Characters Only

  1. Step 1: Replace all non-ASCII characters in the XML file with their corresponding ASCII equivalents.
  2. Step 2: Use a character encoding conversion tool or manually replace each non-ASCII character with its ASCII representation.
  3. Step 3: Verify that the modified XML file can be successfully base64 encoded and downloaded without errors.

Using a Different Base64 Encoding Method

  1. Step 1: Consider using a different base64 encoding method, such as `Buffer.from()` or `TextEncoder` API, which may be able to handle non-ASCII characters.
  2. Step 2: Explore alternative libraries or modules that provide more robust base64 encoding capabilities.
  3. Step 3: Test the modified code with the new encoding method to ensure it resolves the issue.

🎯 Final Words

To resolve this error, you can either replace non-ASCII characters in the XML file with their ASCII equivalents or use a different base64 encoding method. By following these steps, you should be able to successfully download the encoded XML file without errors in Google Chrome.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions