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

Add Example for File Upload #6

Open
anthumchris opened this issue Mar 3, 2018 · 7 comments
Open

Add Example for File Upload #6

anthumchris opened this issue Mar 3, 2018 · 7 comments
Labels
enhancement New feature or request

Comments

@anthumchris
Copy link
Owner

anthumchris commented Mar 3, 2018

Progress indicators for file uploads (specifically indicators for Request objects) would be a great example. At the time of writing, this doesn't seem possible yet, and the Request docs do indicate ReadableStream may be used as a request body

String body: sent

// Post "hello" body.  Browser sends and server responds with what it receives.
fetch(
  new Request('https://dev.anthum.com/post-test/', {
    body: 'hello',
    headers: {
      'content-type': 'text/plain'
    },
    method: 'POST'
  })
)
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));

ReadableStream body: not sent

This may just be bad experimental code on my part or the browsers do not yet support it.

const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(new TextEncoder().encode('hello'));
    controller.close();
  }
});

// Post ReadableStream body. Browser doesn't seem to send, and server responds with nothing received
fetch(
  new Request('https://dev.anthum.com/post-test/', {
    body: stream,
    headers: {
      'content-type': 'text/plain'
    },
    method: 'POST'
  })
)
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
@anthumchris anthumchris added the enhancement New feature or request label Mar 3, 2018
@anthumchris
Copy link
Owner Author

This implementation was also posted for discussion and resolution on Stack Overflow

https://stackoverflow.com/questions/49085424/

@fselcukcan
Copy link

@anthumchris SO says Page Not Found.
What is controller up in the original post.

@anthumchris
Copy link
Owner Author

StackOverflow redirects to this: https://stackoverflow.com/questions/49085424/constructing-fetch-request-with-readablestream-body does that work for you?

MDN has some good info on the controller, and I recommend starting here: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams

@fselcukcan
Copy link

First link is still Not Found but thanks. I will read Streams API on mdn.

@anthumchris
Copy link
Owner Author

anthumchris commented Jun 15, 2019

I wonder if StackOverflow is doing some special geo-based redirection. You could also try searching (with quotes) "Constructing Fetch Request with ReadableStream Body" and click the first result:

image

@aeimi
Copy link

aeimi commented Dec 4, 2022

I wonder if StackOverflow is doing some special geo-based redirection. You could also try searching (with quotes) "Constructing Fetch Request with ReadableStream Body" and click the first result:

Cannot find this in any way.

@anthumchris
Copy link
Owner Author

anthumchris commented Dec 4, 2022

A ReadableStream can now be sent as a fetch() request body (see whatwg/fetch#425). I'll plan on adding an example for upload progress indicators.

@jakearchibald wrote great article Streaming requests with the fetch API. This example shows a server responding with the streaming request it receives:

Demo: https://jsbin.com/piwewop/edit?js,output

fetch(new Request('https://dev.anthum.com/post-stream-test/', {
  body: new Response('🎉').body,  // new ReadableStream
  duplex: 'half',
  method: 'POST',
  headers: { 'Content-Type': 'text/plain' },
}))
  .then(response => response.text())
  .then(console.log)
  .catch(console.error)

_@amdiv, @fselcukcan: Apologies, please ignore original StackOverflow URL (https://stackoverflow.com/questions/49085424/), It was marked as a duplicate question and deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants