gpt4 book ai didi

angular - 如何在 Angular 中使用 HttpProgressEvent 接口(interface)?

转载 作者:行者123 更新时间:2023-12-03 09:01:30 27 4
gpt4 key购买 nike

此处的 API 与 Angular 文档中的相同。但是没有一个例子。我想知道如何使用它。最好有例子。

interface HttpProgressEvent { 
type: HttpEventType.DownloadProgress | HttpEventType.UploadProgress
loaded: number
total?: number
}

最佳答案

官方文档中有一个例子。看看这个:

const req = new HttpRequest('POST', '/upload/file', file, {
reportProgress: true
});

// The `HttpClient.request` API produces a raw event stream
// which includes start (sent), progress, and response events.
return this.http.request(req).pipe(
map(event => this.getEventMessage(event, file)),
tap(message => this.showProgress(message)),
last(), // return last (completed) message to caller
catchError(this.handleError(file))
);


/** Return distinct message for sent, upload progress, & response events */
private getEventMessage(event: HttpEvent<any>, file: File) {
switch (event.type) {
case HttpEventType.Sent:
return `Uploading file "${file.name}" of size ${file.size}.`;

case HttpEventType.UploadProgress:
// Compute and show the % done:
const percentDone = Math.round(100 * event.loaded / event.total);
return `File "${file.name}" is ${percentDone}% uploaded.`;

case HttpEventType.Response:
return `File "${file.name}" was completely uploaded!`;

default:
return `File "${file.name}" surprising upload event: ${event.type}.`;
}
}

Docs

关于angular - 如何在 Angular 中使用 HttpProgressEvent 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49790882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com