0

I'm trying to send an HTTP POST request to a server with the data file uploaded with chunked-transfer encoding. Looking at the example here, it seems like each chunk is actually its own separate XmlHTTPRequest. Is that correct?

Furthermore, I'd like to be able to use a source that is a generator function, instead of a blob or a file (as the data is being created on-the-fly in the browser). Can I modify the source like this and just pass the generator function name instead of the blob?

function* gen() 
{ 
  yield 1;
  yield 2;
  yield 3;
}

xhr.send(gen());
skunkwerk
  • 2,920
  • 2
  • 37
  • 55

1 Answers1

0

Yes the code you posted is posting the data to the server in chunks by making multiple calls to XmlHTTPRequest from the function triggered on the "change" event listener.

Unfortunately I can't see any difference between your code and the version that you've based your version upon. Here's an example to do what you're requesting.

function chunkUploader(fn) {
    var iterator = fn();
    var chunk = iterator.next();
    while (!chunk.done) {
        upload(chunk);
        chunk = iterator.next();
    }
}

You should be able to call this with your function like so.

chunkUploader(gen);
Chris Tomich
  • 133
  • 1
  • 7