Data protocol URL size limitations

后端 未结 11 1456
夕颜
夕颜 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 16:04

    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);
    }
    

提交回复
热议问题