[HTML5] Add easy to use download API.

New `JavaScript.download_buffer` method to create a prompt that let the
user download a file.
This commit is contained in:
Fabio Alessandrelli 2021-05-19 15:51:36 +02:00
parent c311b4c039
commit bf078814cc
8 changed files with 51 additions and 61 deletions

View file

@ -304,6 +304,23 @@ const GodotOS = {
godot_js_os_hw_concurrency_get: function () {
return navigator.hardwareConcurrency || 1;
},
godot_js_os_download_buffer__sig: 'viiii',
godot_js_os_download_buffer: function (p_ptr, p_size, p_name, p_mime) {
const buf = GodotRuntime.heapSlice(HEAP8, p_ptr, p_size);
const name = GodotRuntime.parseString(p_name);
const mime = GodotRuntime.parseString(p_mime);
const blob = new Blob([buf], { type: mime });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = name;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
},
};
autoAddDeps(GodotOS, '$GodotOS');