Data protocol URL size limitations

HtmlCss

Html Problem Overview


Is there any size limitation for "data:" URL scheme values? I'm interested in limitations in popular web browsers. In other words, how long can data:image/jpg;base64,base64_encoded_data be in <img src="data:image/jpg;base64,base64_encoded_data" /> or background-image: url(data:image/jpg;base64,base64_encoded_data)?

Html Solutions


Solution 1 - Html

Short answer: The data URI limit varies.

There are a lot of answers. As the question was asked 5+ years ago, most are now incorrect due to becoming dated, yet this question sits at the top of Google results for "data uri limit". Data URIs are now widely supported and IE 7/8 is no longer a relevant browser. There are many references below because the answer is nuanced today.

Data URI Limits

The data URI spec does not define a size limit but says applications may impose their own.

  • Chrome - 2MB for the current document. Otherwise the limit is the in-memory storage limit for arbitrary blobs: if x64 and NOT ChromeOS or Android, then 2GB; otherwise, total_physical_memory / 5 (source).
  • Firefox - unlimited
  • IE ≥ 9 & Edge - 4GB
  • Safari & Mobile Safari - ?

Alternatives

One technology with a higher limit (500MiB in Chrome) that may be an alternative for your use case is Blob URLs via URL.createObjectURL() using the URL API together with blobs via the File API. An example of that is provided in Using URL.createObjectURL().

A few other alternatives as mentioned in How to write a file / give it to the user are: FileSaver.js, StreamSaver.js, and JSZip.

You can use Modernizr to detect support for data URIs over 32kb.

These answers are pretty much the same as this question, but I mention them to save you the time of reading each one.

Solution 2 - Html

I just made a quick check embedding eight different Jpeg-images ranging from 3,844 to 2,233,076 Bytes in size.

All of the following browsers displayed every image correctly on my Windows 7 (64-bit) system:

  • Chromium 14.0.816.0
  • Firefox 11.0
  • Google Chrome 18.0.1025.142
  • Internet Explorer 9.0.5 (64 Bit)
  • Opera 11.62
  • Safari 5.1.5

Solution 3 - Html

From <http://www.ietf.org/rfc/rfc2397.txt>;:

> The "data:" URL scheme is only useful > for short values. Note that some > applications that use URLs may impose > a length limit; for example, URLs > embedded within <A> anchors in HTML > have a length limit determined by the > SGML declaration for HTML [RFC1866]. > The LITLEN (1024) limits the number of > characters which can appear in a > single attribute value literal, the > ATTSPLEN (2100) limits the sum of all > lengths of all attribute value > specifications which appear in a tag, > and the TAGLEN (2100) limits the > overall length of a tag.

Solution 4 - Html

Solution 5 - Html

2017 answer is: convert data: to blob with function: dataURLtoBlob

function dataURLtoBlob(dataurl) {
	var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
		bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
	while(n--){
		u8arr[n] = bstr.charCodeAt(n);
	}
	return new Blob([u8arr], {type:mime});
}

then create blob url

var temp_url = window.URL.createObjectURL(blob);

then use it in new window if you need.

var redirectWindow = window.open('');
redirectWindow.document.write('<iframe src="' + temp_url + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>')		

works with big files in chrome/firefox 2017

Solution 6 - Html

It is really the "data URI scheme".

As per the Wikipedia page, IE7 lacks support, and IE8 betas limit it to 32kB of data.

Solution 7 - Html

Regarding limitations in web browsers, MSIE 6/7 do not support the data url scheme... More info on wikipedia

The length limits are different per browser - i believe IE8 allows up to 32KB and opera is 4KB, but can't really tell about other browsers...

Solution 8 - Html

Just an FYI, I was able to load a 130K image using a data url in Firefox 3.5 from a JavaScript ajax call. It truncated the image in IE 8, but the whole thing showed up in FF.

Solution 9 - Html

Seems the limit in Firefox 3.6 is 600KB.

Solution 10 - Html

I tried the code from waza123 but the charCodeAt method did not convert all characters correctly. Here is my solution for creating large downloads in the browser. (I used it for JSON data)

function exportToFile(jsonData, fileName) {
    const u8arr = new TextEncoder('utf-8').encode(JSON.stringify(jsonData, null, 2));
    const url = window.URL.createObjectURL(new Blob([u8arr], { type: 'application/json' }));
    const element = document.createElement('a');
    element.setAttribute('href', url);
    element.setAttribute('download', fileName);
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}

Solution 11 - Html

Note: There are some additional restrictions in IE. For iframe is a limit of 4 kb.

> In IE9, the 32kb limit for DataURIs was removed, although for security reasons their use remains limited to certain contexts (specifically, scenarios that create a security context, like IFRAMES, are forbidden)

MSDN

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLicenseQView Question on Stackoverflow
Solution 1 - HtmlTaylor D. EdmistonView Answer on Stackoverflow
Solution 2 - HtmlmiasbeckView Answer on Stackoverflow
Solution 3 - HtmlJohn SaundersView Answer on Stackoverflow
Solution 4 - HtmlNicholas Daley-OkoyeView Answer on Stackoverflow
Solution 5 - HtmljmpView Answer on Stackoverflow
Solution 6 - HtmlJay KominekView Answer on Stackoverflow
Solution 7 - HtmlJ.C. InacioView Answer on Stackoverflow
Solution 8 - HtmlBrad BroermanView Answer on Stackoverflow
Solution 9 - HtmlRiatreView Answer on Stackoverflow
Solution 10 - HtmlselectView Answer on Stackoverflow
Solution 11 - HtmlBruno EberhardView Answer on Stackoverflow