Problem with FS.writeFile

1,002 views
Skip to first unread message

Stefan Meier

unread,
Dec 17, 2014, 9:56:58 AM12/17/14
to emscripte...@googlegroups.com
Hi,

I have a small problem with the following code which aims to download a binary file from a server ans save it to the virtual FS with the FS.writeFile. No matter what I try the file doesn't get written, so perhaps I am doing something substancially wrong. Can anyone provide a hint what I need to correct?

JS code:
                xhr = new XMLHttpRequest();
                xhr
.open("GET", "dtest", true);
                xhr
.responseType = "arraybuffer";
                xhr
.overrideMimeType("application/octet-stream");
                xhr
.onreadystatechange = function () {
                   
if (xhr.readyState === 4 && xhr.status === 200) {
                       console
.log(xhr.response);
                       blob
= new Blob([new Uint8Array(xhr.response)], {type: "application/octet-stream"});
                       console
.log(blob);
                       FS
.writeFile('/dtest', [blob], { encoding: "binary" });
                       console
.log('dtest: '+(FS.stat('/dtest')).size);
                       refreshSGList
();
                   
}
               
};
                xhr
.send();

From the console output I guess that the download from the file is working, the size is the expected:

ArrayBuffer { byteLength: 11667 }
Blob { size: 11667, type: "application/octet-stream" }
"dtest: 1"

But the size that is reported back from FS.stat is wrong and the file is unusable, so I think it is not written eventually.

I am using this with emscripten sdk 1.25 on OSX 10.8.5.

Many thanks for your help!
Stefan

Alon Zakai

unread,
Dec 17, 2014, 2:24:18 PM12/17/14
to emscripte...@googlegroups.com
Can you please make a complete standalone testcase showing the issue, that is runnable in node? Much easier to debug that way.

- Alon


--
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-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan Meier

unread,
Dec 18, 2014, 11:40:23 AM12/18/14
to emscripte...@googlegroups.com

Hi Alon,
thanks for offering your help! I have never worked with node before, but when writing the testcase I recognized the error I had in my source. Just in case somebody has a use for it (downloading custom binary data from a server and storing it to the virtual FS). This is the slightly modified version that works as expected (probably needs to be adapted to responseType blob when all browsers support it):

xhr = new XMLHttpRequest();
xhr
.open("GET", "/msa2/dtest", true);

xhr
.responseType = "arraybuffer";
xhr
.overrideMimeType("application/octet-stream");
xhr
.onreadystatechange = function () {
 
if (xhr.readyState === 4 && xhr.status === 200) {
      console
.log(xhr.response);

      FS
.writeFile('/dtest', new Uint8Array(xhr.response), { encoding: "binary" });

      console
.log('dtest: '+(FS.stat('/dtest')).size);
 
}
};
xhr
.send();



Thanks again for caring!
Stefan

Reply all
Reply to author
Forward
0 new messages