Add file chooser in html.

<input type="file" id="file-input" style="color:white;" />

Choose file and convert the data to uint array, use FS library provided by Emscripten to write it to wasm virtual file system.
Note: we have to add FS in EXPORTED_RUNTIME_METHODS in CMakeLists.txt for CPlusPlus project.

javascript code:

 function readSingleFile(e) {
   var file = e.target.files[0];
   fileName = file.name;

   var reader = new FileReader();
   reader.onload = function(e) {
       var contents = e.target.result;
       const int8a = new Uint8Array( contents );
       console.log( int8a );

       Module['FS'].writeFile( fileName, int8a );

       // to do something:
   };
   reader.readAsArrayBuffer(file);  // Blob object
 }

 document.getElementById('file-input')
   .addEventListener('change', readSingleFile, false);

The file had been in the virtual file system after Module['FS'].writeFile.
Relative post: https://www.weiy.city/2021/12/open-file-by-browser-set-mouse-position-by-vtk/

Categories: Web

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

File Charset Converter
: help you to convert character encoding of file.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x