我使用答案中的代码在角度5中实现了文件下载:

file dowload using filesaver or windows.url

我有一个spring boot后端,它发送一个http响应对象,它有文件字节数组作为主体和内容类型,内容配置和内容长度 . 我不得不允许暴露的 Headers ,我可以得到 Headers . 如何在下载过程中使用它在浏览器中显示文件下载大小?它们是我设置大小的API,浏览器会在下载文件时显示它 .

从后端获取报告文件的代码:

this.dataService.getReportFile(this.reportForm.get('date').value, params).subscribe(resp => {
      let filename = resp.headers.get("content-disposition").split(';')[1].split('filename')[1].split('=')[1].trim();
      this.downloadFile(resp.headers.get("content-length"), resp.body, resp.headers.get("content-type"), filename);
    });

下载文件的代码:

downloadFile(fileSize: string, data: any, reportType: any, fileName: any) {
    const blob = new Blob([data], { type: reportType });
    FileSaver.saveAs(blob, fileName);
}

正如您所看到的,我在这里传递fileSize但现在确定如何使用它以便浏览器显示文件大小 .