Data protocol URL size limitations

后端 未结 11 1334
夕颜
夕颜 2020-11-22 15:01

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;base6

相关标签:
11条回答
  • 2020-11-22 15:39

    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

    0 讨论(0)
  • 2020-11-22 15:44

    Seems the limit in Firefox 3.6 is 600KB.

    0 讨论(0)
  • 2020-11-22 15:45

    I've read that Safari has a limit of 128K for data URIs:

    http://blog.clawpaws.net/post/2007/07/16/Storing-iPhone-apps-locally-with-data-URLs#c1989348

    And Chrome is 2M:

    http://code.google.com/p/chromium/issues/detail?id=44820#c1

    0 讨论(0)
  • 2020-11-22 15:46

    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.

    Related Questions

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

    • getting max Data-Uri size in Javascript
    • “Aw, Snap” when data uri is too large
    • What is the size limit of a Base64 DataURL image?
    • What is the maximum length of a URL in different browsers? This question is about URLs, not data URIs, but there are relevant answers and comments about data URIs.
    • Is it possible to programmatically detect size limit for data url?
    0 讨论(0)
  • 2020-11-22 15:59

    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...

    0 讨论(0)
  • 2020-11-22 16:00

    It is really the "data URI scheme".

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

    0 讨论(0)
提交回复
热议问题