Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting max Data-Uri size in Javascript

I want to export data as a Data URI, only if the size of the data doesn't exceed the max size of the Data URI supported by the current browser is bigger than the file I want to download.

document.location.href= "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"+data

This sometimes results in a Browser error when the file is too big, but I wish that the error could be catched before the download (to try an other method for example).

How can I get the max Data-URI size in JS ?

like image 369
edi9999 Avatar asked Jun 13 '13 08:06

edi9999


1 Answers

I have decided to hardcode this table:

Here's the script I use to test the maxsize:

String.prototype.repeat = function( num )
{
    return new Array( num + 1 ).join( this );
}

testDataURI=function(size) 
{
    window.open("data:text/plain,"+"a".repeat(size-16))
}


testDataURI(100000) //test 100k characters

JSFIDDLE

Results:

  1. Chrome (as of version 28): works with 2 097 152 Bytes, which is exactly 2 MB
  2. Firefox (as of version 26): works with 1 040 000 Bytes, which is probably 1 MB
like image 57
edi9999 Avatar answered Sep 30 '22 04:09

edi9999