Ky: Add upload progress callback

Created on 5 Oct 2019  路  8Comments  路  Source: sindresorhus/ky

Hello, I would like to add an image loading indicator. Is this possible when using this library?

enhancement help wanted

Most helpful comment

I suppose if the body is just a plain stream, we'd only be able to provide how many bytes have been uploaded but not the total bytes, which may not be super useful.

However, if the body is a Blob or a File (or anything that can be normalized to one of those), then we can use blob.size to get the total bytes. And then we can use blob.stream() to get a ReadableStream for it.

All 8 comments

Did you try the onDownloadProgress callback?

Sorry, it seems I did not ask the question correctly. I need upload progress, not download progress. And onDownloadProgress doesn't help me.

I see. We don't have that feature at the moment, but it would be nice to add. I think it could be done similarly to download progress.

It cannot. ky uses fetch under the hood, which uses streams, which does not support upload progress callbacks.

Seems like a pretty bizarre thing to leave out of the spec, if you ask me. Until they're added, ky cannot support them unless it falls back to XMLHttpRequest.

I realize that fetch doesn't have a callback for upload progress, which is a shame. But I think we should be able to detect when options.body is a stream and then send it through a passthrough stream of our own that reads the chunks manually, similarly to how our onDownloadProgress option works.

As long as we can determine the total bytes to be uploaded then absolutely :)

I suppose if the body is just a plain stream, we'd only be able to provide how many bytes have been uploaded but not the total bytes, which may not be super useful.

However, if the body is a Blob or a File (or anything that can be normalized to one of those), then we can use blob.size to get the total bytes. And then we can use blob.stream() to get a ReadableStream for it.

Really missing this for File uploads. You possible solution sound great @sholladay .

Was this page helpful?
0 / 5 - 0 ratings