think49さんの推測通り、Fetch APIには、おそらくイベントの概念はないのではないでしょうか。
どちらかというと、Stream
などを用いて、Fetch APIを使ってprogress イベントを実装するようなイメージではないかと思います。
upload progress? · Issue #21 · whatwg/fetch
に、以下のようにあるところから判断しました。
The native fetch() API implemented by browsers does not use XMLHttpRequest underneath.
The eventual API for progress is building something on https://streams.spec.whatwg.org/
which https://github.com/yutakahirano/fetch-with-streams/ is adding to fetch().
ダウンロード(応答)の進捗については、
Fetch Standard (日本語訳) 6. fetch API
に記述されている
ボディデータを漸進的に受信したいなら:
という箇所にあるコードをもとにすると、一応プログレスバーに進捗は出すことができました。
(ファイルの内容は読み捨ててますが。。)
lang
1fetch('test.txt').then(function(response) {
2 var contentLength = response.headers.get("Content-Length")
3 consume(response.body.getReader(), contentLength);
4});
5
6function consume(reader, contentLength) {
7 var progressBar = document.getElementById('download');
8 var total = 0;
9 return pump();
10
11 function pump() {
12 reader.read().then(function(result) {
13 var done = result.done;
14 var value = result.value;
15 if (done) {
16 progressBar.value = total;
17 return;
18 }
19 total += value.byteLength;
20
21 progressBar.max = contentLength;
22 progressBar.value = total;
23
24 return pump();
25 })
26 }
27}
Fetch API で出来ない事
Fetch Standard (日本語訳) 6. fetch API
fetch() メソッドは、リソースを fetch するための,比較的 低レベルの API である。
XMLHttpRequest より少しばかり基盤的部分を対象にする
— 現時点では、いつ要請の進捗(応答の進捗ではなく)が来たかを報告する機能を欠いているが。
とあるところから、アップロード(要請)の進捗については、現状その機能がないようです。
参考リンク
同じように困っている人の質問です。
Now that I think about it, I couldn't find any API related to download/upload progress events with the Fetch API. If that's the case, it means it's an actual regression to XMLHTTPRequest.
You can access the stream as it's coming in using fetch - would that let you do progress? Seems like it would.
未確認ですが、Fetch APIの制限事項が書かれています。
Limitations
- Promises don't have finally/always method- there is a workaround
- There is neither abort method nor a timeout property for fetch()
- fetch() (as Promise- based standard) doesn't have ability to provide onProgress() callback- so you cannot process a response by chunks
- Haven't found clear info about doing synchronous fetching
but, most of all, it's not provided, because even in such case onSuccess and onReject callbacks would be invoked on the next tick- due to Promise nature.
これも、同じように困っている人の質問です。
Soon you'll be able to provide a stream as the body of a request, but the open question is whether the consumption of that stream relates to bytes uploaded.
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/07 13:09
2016/07/07 22:33
2016/07/08 22:35