ky icon indicating copy to clipboard operation
ky copied to clipboard

Add upload progress callback

Open psixdev opened this issue 4 years ago • 16 comments

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

psixdev avatar Oct 05 '19 15:10 psixdev

Did you try the onDownloadProgress callback?

sholladay avatar Oct 06 '19 02:10 sholladay

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

psixdev avatar Oct 06 '19 06:10 psixdev

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.

sholladay avatar Oct 07 '19 01:10 sholladay

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.

Qix- avatar Nov 12 '19 09:11 Qix-

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.

sholladay avatar Nov 12 '19 17:11 sholladay

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

Qix- avatar Nov 12 '19 18:11 Qix-

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.

sholladay avatar Nov 12 '19 21:11 sholladay

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

aldipower avatar Jun 08 '20 12:06 aldipower

can this be added yet? axios seems to have this feature.

SupertigerDev avatar Dec 09 '20 11:12 SupertigerDev

@supertiger1234 You are perfectly allowed to submit a PR, yes :)

Qix- avatar Dec 09 '20 13:12 Qix-

🤷 I wouldn't even be using a lib for fetching if I knew how. I am using axios only for when uploading files. Technically i fixed my problem.

SupertigerDev avatar Dec 09 '20 18:12 SupertigerDev

can this be added yet? axios seems to have this feature.

No, I don't think axios supports it for browser. It is only supported in nodejs. Right?

shresthapradip avatar Mar 31 '21 14:03 shresthapradip

Please make this :D

IRediTOTO avatar Dec 11 '21 05:12 IRediTOTO

For anyone who wants to try implementing this, take a look at https://github.com/sindresorhus/ky/pull/34 and use that as a reference. Once there's a rough draft, I'd be happy to review and help with the code.

sholladay avatar Jan 12 '22 21:01 sholladay

It seems this should now be possible with Chromium versions > 105. It would be great if it could be incorporated into ky!

https://developer.chrome.com/articles/fetch-streaming-requests/#streaming-request-bodies fetch() upload streaming - Chrome Platform Status (chromestatus.com)

Here's an example implementation: https://stackoverflow.com/a/52860605/19510854

nickchomey avatar Dec 01 '22 13:12 nickchomey

It seems this should now be possible with Chromium versions > 105. It would be great if it could be incorporated into ky!

I've looked into this a bit more and it wouldn't be as easy to implement in ky. The presence of onUploadProgress would have to force the passing of duplex: 'half' to underlying fetch and force the body to be a stream. If you already have the entire file, you have to manually push it through a stream again to count the bytes. When uploading FormData, this would result in hacky conversions which I haven't gotten to work at all. Further more, supporting onUploadProgress would mean dropping support for HTTP/1.1, which frankly is unacceptable. Even giants like AWS S3 still only support uploading over HTTP/1.1.

All in all, I don't see an addition like this making sense until fetch itself exposes more control over this (such as some sort of fetch observer)

Murderlon avatar Oct 12 '23 14:10 Murderlon