save file to MEMFS and download it

1,862 views
Skip to first unread message

thebo...@gmail.com

unread,
Dec 28, 2016, 4:53:59 AM12/28/16
to emscripten-discuss
I have c++ code that create new file, for example:

#include <stdio.h>
int main ()
{
  FILE
* pFile;
  pFile
= fopen ("c:\myfile.txt","w");
 
if (pFile!=NULL)
 
{
    fputs
("hey",pFile);
    fclose
(pFile);
 
}
 
return 0;
}


the code run with emscripten and I want to save it to MEMFS and then download it to my hardrive.
I saw a lot of posts about it but didn't really get how to do it.
What exactly need to be the path to save it?
how to download it after it saved?

thanks for help

Flix

unread,
Dec 28, 2016, 6:24:08 AM12/28/16
to emscripten-discuss
I  needed this myself, together with a way to move files from my local HD to the root to the emscripten file system (to complete the file download/upload round trip)..

In the end I've managed to do it by looking how other projects were made (I'm not a Javascript programmer...).

However, to address your question, the way I've found is to use a custom emscripten html shell that includes the file FileSaver.js available here:
https://github.com/eligrey/FileSaver.js, together with some code that I call from C++.
The shell file I use ends like this:

<script src="FileSaver.js"> </script>
<script>
  function saveFileFromMemoryFSToDisk(memoryFSname,localFSname)     // This can be called by C++ code
  {
     var data=FS.readFile(memoryFSname);
     var blob;
     var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
     if(isSafari) {
        blob = new Blob([data.buffer], {type: "application/octet-stream"});
     } else {
        blob = new Blob([data.buffer], {type: "application/octet-binary"});
     }
     saveAs(blob, localFSname);
  }
</script>
 
   {{{ SCRIPT }}}  
</body>
</html>
 
Then in my C++ code I can use for example:
emscripten_run_script("saveFileFromMemoryFSToDisk('images/image.jpg','image.jpg')");
// The second argument must be a simple filename (we can't use directories)

Hope this helps.

P.S. AFAIR I copied this solution from this emscripten program here: http://webloria.loria.fr/%7Elevy/GEOGRAM/



thebo...@gmail.com

unread,
Dec 28, 2016, 6:45:01 AM12/28/16
to emscripten-discuss
I'm not sure I understand the part of the HTML and JS and how I handle everything together?
should I run it after the c++ end?
there is somewhere I find the whole code?

בתאריך יום רביעי, 28 בדצמבר 2016 בשעה 13:24:08 UTC+2, מאת Flix:

Flix

unread,
Dec 28, 2016, 10:22:32 AM12/28/16
to emscripten-discuss


I'm not sure I understand the part of the HTML and JS and how I handle everything together?
should I run it after the c++ end?

Nope, you have to keep FileSaver.js in the same folder and you have to use an emscripten shell through --shell-file (see: https://kripken.github.io/emscripten-site/docs/tools_reference/emcc.html).
 
there is somewhere I find the whole code?
In my Dear ImGui fork here https://github.com/Flix01/imgui you can find everything you need.
See this folder: https://github.com/Flix01/imgui/tree/2015-10-Addons/examples/addons_examples/html
And the end of this document https://raw.githubusercontent.com/Flix01/imgui/2015-10-Addons/examples/addons_examples/README_FIRST.txt for further info (search for: Using predefined shells).


 

Flix

unread,
Dec 30, 2016, 5:33:10 AM12/30/16
to emscripten-discuss
Have you solved it ?

Jukka Jylänki

unread,
Jan 5, 2017, 5:45:33 AM1/5/17
to emscripte...@googlegroups.com
Hey this is a pretty neat feature! I think I'll look at introducing the same to this filesystem profiler I'm working on.

Btw if one is using the emrun tool to automate debugging runs, there is a special runtime function "emrun_file_dump(filename, data)" which allows one to programmatically dump a file out to local hard drive. This can make creating test harnesses easier, although it's not a feature for end user scenarios.

2016-12-30 12:33 GMT+02:00 Flix <filma...@gmail.com>:
Have you solved it ?

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Flix

unread,
Jan 5, 2017, 11:05:03 AM1/5/17
to emscripten-discuss

Btw if one is using the emrun tool to automate debugging runs, there is a special runtime function "emrun_file_dump(filename, data)" which allows one to programmatically dump a file out to local hard drive. This can make creating test harnesses easier, although it's not a feature for end user scenarios.


Good to know.
I still haven't tried to create a local files directly from a C++ byte array (I just needed to clone files locally from the memory file system).
Creating local files on the fly from a byte array might be useful too.
Reply all
Reply to author
Forward
0 new messages