<input id="file-input" type="file" name="name" style="display: none;"/>
emscripten_run_script( (char*)((std::string) "document.getElementById('file-input').click(); " + "var fileCont; " + "var reader = new FileReader(); " + "reader.onloadend = function(evt) { " + " if (evt.target.readyState == FileReader.DONE) { " + " fileCont = evt.target.result; " + " } " + " var arrayBuffer = this.result, " + " array = new Uint32Array(arrayBuffer), " + " binaryString = String.fromCharCode.apply(null, array); " + " Module.ccall('EM_gotOpenFileName', null, ['string', 'string', 'number'], [document.getElementById('file-input').value, binaryString, arrayBuffer.byteLength]); " + "}; " + "reader.readAsArrayBuffer(document.getElementById('file-input').files[0]); " ).c_str());
extern "C"{ EMSCRIPTEN_KEEPALIVE void EM_gotOpenFileName(char* fileName, char* cont, int len) { // Store the file in a temporary location so we can read it later if we need to. // JavaScript doesn't allow us to grab it's location, so we must do it this way. std::string tempFile = fileName;
// Getting the file's name without any directories. i.e "C:/fakepath/file.png" will turn into "file.png" std::vector<std::string> tkns = findTokens("/", tempFile); tempFile = tkns[tkns.size()-1]; tkns = Operations::findTokens("\\", tempFile); tempFile = tkns[tkns.size()-1]; tempFile = (std::string)".temp/" + tempFile;
writeFile(tempFile, cont, len); // write the file. assume this works fine using fwrite.
// ... }
}
emscripten_run_script( (char*)((std::string) "var fileName = 'em_file.txt'; " + "var text = Module.ccall('EM_readFile', 'string', ['string'], [fileName]); " + // EM_readFile returns a char* of the file content "var blob = new Blob([text], {type: \"text/plain;charset=utf-8\"}); " + "saveAs(blob, fileName+\".txt\");" ).c_str() );
Any ideas?