angular - 根据文件上传百分比显示 mat-spinner

标签 angular azure angular-material angular6 azure-blob-storage

我正在使用Azure Blob存储执行文件上传操作,现在我可以将文件上传到Azure Blob,但是在上传时我想要显示的文件 mat-spinner 用户,因为上传需要几秒钟。

下面是组件代码:

HTML

<img src="{{Url+storageToken}}" height="100px" width="100px">
<input type="file" (change)="onFileChange($event)" >
<mat-spinner></mat-spinner>
<div *ngIf="filesSelected">
  <pre>{{uploadProgress$ | async | json}}</pre>
</div>

TS

import { Component } from '@angular/core';
import { from, Observable } from 'rxjs';
import { combineAll, map } from 'rxjs/operators';
import { BlobService } from './az-storage/blob.service';
import { ISasToken } from './az-storage/azure.storage';

interface IUploadProgress {
  filename: string;
  progress: number;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  uploadProgress$: Observable<IUploadProgress[]>;
  filesSelected = false;
  Url: string;
  fileName: string;
  storageToken: string = '......storageAccessToken................';

  constructor(private blobStorage: BlobService) {}

  onFileChange(event: any): void {
    this.filesSelected = true;

    this.uploadProgress$ = from(event.target.files as FileList).pipe(
      map(file => this.uploadFile(file)),
      combineAll()
    );
    console.log(File);
  }

  uploadFile(file: File): Observable<IUploadProgress> {
    const accessToken: ISasToken = {
      container: 'upload-demo',
      filename: 'users/' + file.name,
      storageAccessToken:
        '......storageAccessToken................',
      storageUri: 'https://file-upload.blob.core.windows.net/'
    };

    this.fileName = file.name;
    this.Url = `https://file-upload.blob.core.windows.net/upload-demo/users/${this.fileName}`;

    return this.blobStorage
      .uploadToBlobStorage(accessToken, file)
      .pipe(map(progress => this.mapProgress(file, progress)));
  }

  private mapProgress(file: File, progress: number): IUploadProgress {
    return {
      filename: file.name,
      progress: progress <================ When it becomes 100%
    };
  }


}

现在我在模板中显示文件名上传%,例如 这: enter image description here

预期结果:

正如在模板中一样,当调用 onFileChange 事件时,我想启用 spinner 并且当 upload % 为 100% 时(i,e当进度为 100% 时)我想关闭 spinner

最佳答案

这里需要使用mat-progress-spinner更新spinner中的文件上传进度。

示例 -

your.component.html

<div *ngIf="uploadProgress !==0 &&  uploadProgress !== 100" >
     <mat-progress-spinner class="example-margin" 
         [color]="color" [mode]="mode" [value]="uploadProgress">
    </mat-progress-spinner>
</div>

<div *ngIf="uploadProgress === 100" >
  <h1>File  upload successfully!</h1>
</div>

your.component.ts

uploadProgress = 0; // declare class level variable
...

private mapProgress(file: File, progress: number): IUploadProgress {
    this.uploadProgress = progress;// updating progresss of mat-spinner
    return {
      filename: file.name,
      progress: progress <================ When it becomes 100%
    };
  }

<强> Demo on stackblitz

希望这会有所帮助!

关于angular - 根据文件上传百分比显示 mat-spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135323/

相关文章:

javascript - 从 typescript 编译后,此关键字在浏览器中给出空引用错误

Angular 7 react 形式绑定(bind)拦截

javascript - 我可以创建单独的 ts 文件来为 Angular 4 应用程序中的所有组件编写业务逻辑吗?

azure - 如何在json文件中引用环境变量

c# - Windows Azure 角色内缓存异常问题

angular - 当用户在 mat 选项卡之间切换时重新启动表单

Angular4 Material 芯片组+自动完成

Angular 5 - 如何生成定义文件

azure - 扩展 Azure SQL 弹性池存储时是否会出现停机?

css - Angular 8 : Change Height in Mat-form-field to Specific pixel number