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 <[email protected]> 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)

-- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to