Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload progress return response.json()? #395

Closed
borm opened this issue Sep 17, 2016 · 1 comment
Closed

upload progress return response.json()? #395

borm opened this issue Sep 17, 2016 · 1 comment

Comments

@borm
Copy link

borm commented Sep 17, 2016

Hi, i have readed
whatwg/fetch#21
#202
#290
#379

And this issue copy whatwg/fetch#389

How get response json from server after progress upload

function consume(response, total=0) {
  var reader = response.body.getReader()
  return pump()
  function pump() {
    return reader.read().then(({done, value}) => {
      if (done) {
        return response
      }
      total += value.byteLength
      log(`received ${value.byteLength} bytes (${total} bytes in total)`)
      return pump()
    })
  }
}

fetch("/music/pk/altes-kamuffel.flac", {method: 'POST', body: MYFormData})
  .then(response => consume(response))
  .then(response => return response.json() // "return Already read" )
  .then((resultFromServer) => log(resultFromServer.file)) 
  .catch(e => log("something went wrong: " + e))
@dgraham
Copy link
Contributor

dgraham commented Sep 17, 2016

In this example, you're uploading a form to an audio URL, then reading a streaming response body from the server, then consuming the response again as a JSON document. An HTTP response body may only be consumed a single time.

This fetch polyfill does not implement response body streaming, so you'll want to use Chrome for testing that.

@dgraham dgraham closed this as completed Sep 17, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants