【问题标题】:Loop through files to upload doesn't work循环上传文件不起作用
【发布时间】:2019-11-01 19:35:15
【问题描述】:

我正在尝试在 Angular 中创建(多)文件上传。我希望用户选择一个或多个文件,调用我的后端来获取 Azure Blob 存储的共享访问签名,然后将文件从客户端上传到 Azure 存储。

我的问题是,在选择文件后,我想循环遍历选定的文件并调用每个文件的上传过程。

onFileChange(event: any): void {
  this.filesSelected = true;
  this.uploadProgress$ = from(event.target.files as FileList).pipe(
    map(file => this.uploadFile(file)),
    combineAll()
  );
}

我在这里的示例中找到了上面的代码:https://medium.com/@stuarttottle/upload-to-azure-blob-storage-with-angular-7977e979496a,这似乎是一个很好的解决方案。我调整了代码,因此我为每个文件检索了一个 SAS,并将其他所有内容保持原样。我的问题是地图没有归档。我选择一个或多个文件都没有关系。调试显示所选文件确实到达(event.target.files 实际上是一个带有所选文件的 FileList),但 map 运算符根本不会触发。查看代码我找不到问题。也许你们可以帮忙;)

[编辑] 在 cmets 中添加了 uploadFile 请求

uploadFile(file: File): Observable<IUploadProgress> {
  return this.picturesService.acquireSas(file.name).pipe(
    flatMap(sas => {
      const accessToken: ISasToken = {
        container: sas.containerName,
        filename: file.name,
        storageAccessToken: sas.valetKeySecret,
        storageUri: sas.storageUrl
      };
      return this.blobStorage
        .uploadToBlobStorage(accessToken, file)
        .pipe(map(progress => this.mapProgress(file, progress)));
    })
  );
}

[/编辑]

【问题讨论】:

  • 可以添加uploadFile实现吗?
  • 是的,但我不知道为什么这有助于回答问题

标签: angular azure rxjs azure-storage azure-blob-storage


【解决方案1】:

你所做的一切都很好。你只需要订阅 observable。

点赞this.uploadProgress$.subscribe();

工作示例here

可以了解rxjs@learnrxjs.io

【讨论】:

    【解决方案2】:

    通过数组映射怎么样:

    this.arrayOfUploadProgress = [...event.target.files]
      .map(file => this.uploadFile(file));
    forkJoin(this.arrayOfUploadProgress).subscribe(response => console.log(response));
    

    【讨论】:

    • ERROR TypeError: event.target.files.map is not a function
    • 对不起,我没有机会尝试。你现在可以试试。
    • 似乎把我带到了某个地方。遇到一些不同的错误,让我检查一下,我会排除你的答案。感谢您的帮助!
    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2013-12-09
    相关资源
    最近更新 更多