function offerFileAsDownload(filename, mime) {
mime = mime || "application/octet-stream";
let content = Module.FS.readFile(filename);
console.log(`Offering download of "${filename}", with ${content.length} bytes...`);
var a = document.createElement('a');
a.download = filename;
a.href = URL.createObjectURL(new Blob([content], {type: mime}));
a.style.display = 'none';
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
}, 2000);
}
运行后调用函数(选项1):
<script>
// This must go BEFORE sourcing main.js:
var Module = {
onRuntimeInitialized: () => {
Module.postRun.push(() => offerFileAsDownload("filename.ext", "mime/type"));
}
};
</script>
<script src="main.js"></script>
2条答案
按热度按时间enyaitl31#
明白了
yeotifhr2#
我已经发布了一个小型的只包含头文件的C++库,它允许在Emscripten中下载和上传文件:
https://github.com/Armchair-Software/emscripten-browser-file
这比另一个答案简单得多,只需使用
EM_JS
-下面的示例来自上面的示例:这适用于任何内存缓冲区,-如果您想特别引用MEMFS,可以使用
Module.FS.readFile(filename)
。