【问题标题】:Javascript/Angular download method is only downloading 1kbJavascript/Angular 下载方式只下载 1kb
【发布时间】:2021-06-25 03:53:16
【问题描述】:

我有一个安全的 AWS url,它应该返回一个文件以供下载。但是,下载的文件只有 1kb 大小。当我在检查员“网络”选项卡中查看该呼叫的“预览”时,我看到了图像。就在下载结果时,它基本上根本就没有文件。下载功能在订阅 http 请求后发生:

  downloadFile(file: string) {
    this.awsService
      .getDownloadUrl(file, poNumber, client)
      .pipe(takeUntil(this.unsubscribe))
      .subscribe(resp => {
        if (resp && resp.url) {
          this.awsService.downloadFile(resp.url).subscribe(data => {
            const navigator = this.window.navigator;
            const isIE = /msie\s|trident\//i.test(navigator.userAgent);

            if (isIE) {
              navigator.msSaveOrOpenBlob(data.Body, file);
            } else {
              const blob = new Blob([data.Body], { type: data.type });
              const link = document.createElement('a');
              link.href = this.window['URL'].createObjectURL(blob);
              link.download = file;
              link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: this.window }));
            }
          });
        }
      });
  }

控制台或 api 响应中没有错误。

【问题讨论】:

  • 您通常没有嵌套订阅,您通常使用扁平化运算符,例如 (merge/concat)Map。这可能是一个原因
  • @Nico 这不是问题,但感谢您的清理建议。这是遗留代码,所以旧的开发人员没有像他们应该做的那样做。

标签: javascript angular amazon-web-services observable


【解决方案1】:

对我来说,问题是const blob = new Blob([data.Body], { type: data.type }); 我不需要使用data.Body,因为所需的值已经是data,而对象中不存在Body。显然,即使对象中不存在 Body,此请求也不会触发运行时错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2020-05-31
    相关资源
    最近更新 更多