emscripten_async_wget2_data

238 views
Skip to first unread message

Joel Croteau

unread,
Mar 3, 2014, 12:09:37 AM3/3/14
to emscripte...@googlegroups.com
I would like to be able to load a file from a URL directly into memory, while having the functionality of emscripten_async_wget2. Would it be possible to have this implemented?

Rob Raguet-Schofield

unread,
Mar 3, 2014, 8:16:29 AM3/3/14
to emscripte...@googlegroups.com
I also needed this functionality, but I was unable to make it work with the built-in functions. I added my own modified version of this function. Here's the JS implementation:


mergeInto(LibraryManager.library, {
IOJSURLRequest : function(url, method, param, arg, onload, onerror, onprogress) {
var _url = Pointer_stringify(url);
var _method = Pointer_stringify(method);
var _param = Pointer_stringify(param);

var http = new XMLHttpRequest();
http.open(_method, _url, true);
http.responseType = 'arraybuffer';

http.onload = function(e) {
if (http.status == 200) {
if (onload) {
var byteArray = new Uint8Array(http.response);
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
Runtime.dynCall('viii', onload, [arg, buffer, byteArray.length]);
_free(buffer);
}
} else {
if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]);
}
};
http.onerror = function(e) {
if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]);
};
http.onprogress = function(e) {
var percentComplete = (e.position / e.totalSize)*100;
if (onprogress) Runtime.dynCall('vii', onprogress, [arg, percentComplete]);
};

if (_method == "POST") {
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(_param);
} else {
http.send(null);
}
}
});


Then you can call the function from C/C++:


extern "C" void IOJSURLRequest(const char* url, const char* method, const char* param, void* context, void (*onload)(void* context, const void* data, int len), void (*onerror)(void* context, int error), void (*onprogress)(void* context, int progress));


And build with --js-library foo.js



On Mar 2, 2014, at 11:09 PM, Joel Croteau <jcro...@gmail.com> wrote:
> I would like to be able to load a file from a URL directly into memory, while having the functionality of emscripten_async_wget2. Would it be possible to have this implemented?


Rob Raguet-Schofield
(rob ra gA skO fEld)

Alon Zakai

unread,
Mar 3, 2014, 1:39:23 PM3/3/14
to emscripte...@googlegroups.com
Pull request would be welcome.

- 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/groups/opt_out.

Joel Croteau

unread,
Mar 3, 2014, 7:31:44 PM3/3/14
to emscripte...@googlegroups.com
Ok, I made my own implementation of this function and sent a pull request https://github.com/kripken/emscripten/pull/2180
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages