最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I load a file from a HTML input into Emscripten's MEMFS file system? - Stack Overflow

programmeradmin4浏览0评论

I have an HTML input like this <input type="file" id="myfile" onchange="load_file()">. I want to load the file into Emscripten's MEMFS file system. Looking at the Emscripten file API I have been trying to use FS.mount() to do so.

However, the documentation only gives an example of using mount using WORKERFS, so I tried playing around with it like this.

let files = document.getElementById('myfile').files;
let file=files[0];
FS.mount(MEMFS, {files: file },'test');
console.log(FS.readFile('test/' + file.name));

But get a "No such file or directory" error message when I try to read the file.

Any suggestions of where I am going wrong?

I have an HTML input like this <input type="file" id="myfile" onchange="load_file()">. I want to load the file into Emscripten's MEMFS file system. Looking at the Emscripten file API I have been trying to use FS.mount() to do so.

However, the documentation only gives an example of using mount using WORKERFS, so I tried playing around with it like this.

let files = document.getElementById('myfile').files;
let file=files[0];
FS.mount(MEMFS, {files: file },'test');
console.log(FS.readFile('test/' + file.name));

But get a "No such file or directory" error message when I try to read the file.

Any suggestions of where I am going wrong?

Share Improve this question asked Apr 29, 2020 at 7:56 Harry O'ConnorHarry O'Connor 1631 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Here is what I ended up doing–

input_test.html for file input:

<html>
    <body>
        <input type="file" id="myfile" onchange="load_file()"> 
    </body>
    <script type="text/javascript" src="auto_em.js"></script>
    <script type="text/javascript" src="input_test.js"></script>
</html>

input_test.js then reads the file as an ArrayBuffer and then uses a Uint8Array view of that buffer to write the file to Emscripten's MEMFS:

let reader= new FileReader();

function load_file(){
    let files = document.getElementById('myfile').files;
    let file=files[0];
    reader.addEventListener('loadend', print_file);
    reader.readAsArrayBuffer(file);
}

function print_file(e){
    let result=reader.result;
    const uint8_view = new Uint8Array(result);

    FS.writeFile('write_test.txt', uint8_view)
    Moduleall('print_file', 'number', ['string'], ['write_test.txt'])
}

print_input.cpp to print out file to check it worked:

#include <stdio.h>
extern "C" {

int print_file(char* file_path){

    FILE *file = fopen(file_path, "r");
    if (!file) {
        printf("cannot open file\n");
        return 1;
    }
    while (!feof(file)) {
        char c = fgetc(file);
        if (c != EOF) {
            putchar(c);
        }
    }
    return 0;
}
}

then piled using:

emcc print_input.cpp -o auto_em.js -s "EXPORTED_FUNCTIONS=['_print_file']" -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall']" -s FORCE_FILESYSTEM=1
发布评论

评论列表(0)

  1. 暂无评论