Angular 从REST api下载具有不同文件类型的文件

Angular 从REST api下载具有不同文件类型的文件,angular,spring-boot,Angular,Spring Boot,您好,我正在尝试在轻型版本中实现一些filemanager。 在使用Java Spring Boot/Rest API的服务器端Im上,方法如下所示: InputStream inStream = fmService.downloadFile("...url..." + path); response.setHeader("Content-Disposition", "attachment; filename=" + file

您好,我正在尝试在轻型版本中实现一些filemanager。 在使用Java Spring Boot/Rest API的服务器端Im上,方法如下所示:

InputStream inStream = fmService.downloadFile("...url..." + path);
    
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
org.apache.commons.io.IOUtils.copy(inStream, response.getOutputStream());
response.getOutputStream().flush();
downloadFile() {
    const headers = new HttpHeaders({'responseType': 'blob'}); //also tryed array buffer or text

    this.http.get("http://localhost:8080/file?path=/Documents/Nextcloudflyer.pdf&filename=Nextcloudflyer.pdf", 
    {headers: headers}).subscribe((res) => {
      console.log("RES: ", res);
    });
}
前端使用角度传感器。我试图从url获取的内容如下:

InputStream inStream = fmService.downloadFile("...url..." + path);
    
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
org.apache.commons.io.IOUtils.copy(inStream, response.getOutputStream());
response.getOutputStream().flush();
downloadFile() {
    const headers = new HttpHeaders({'responseType': 'blob'}); //also tryed array buffer or text

    this.http.get("http://localhost:8080/file?path=/Documents/Nextcloudflyer.pdf&filename=Nextcloudflyer.pdf", 
    {headers: headers}).subscribe((res) => {
      console.log("RES: ", res);
    });
}
使用downloadFile()时,我现在遇到以下错误:

第一个问题是:尽管我尝试使用pdf,但我需要它适用于所有文件类型。但具体如何?(听说过类似于八位字节流类型的东西吗?但无法让它工作。)有没有一种简单的方法,或者我真的必须像我有时看到的那样单独处理每个文件类型


第二个问题当然是错误:这里发生了什么?我在响应中加入了ByTestStream,看起来Angular得到了它(get是代码200,所以看起来还可以)。但出了什么问题?我是不是用一种错误的方式建造了ByteArray?或者某些标题设置错误或被遗忘了?

当您直接访问文件链接时会发生什么情况?设法使其在PDF中工作:response.setHeader(“内容类型”,“应用程序/PDF”);response.setHeader(“内容处置”、“附件;文件名=“+filename”);org.apache.commons.io.IOUtils.copy(inStream,response.getOutputStream());response.getOutputStream().flush();流内关闭();但第一个问题:我无法轻松获取内容类型(唯一的想法是:打开文件名结尾,但是,嗯…)。第二个问题:文件名现在是一些神秘的id,尽管文件可以下载并打开。当您直接访问文件链接时会发生什么情况?设法使其适用于PDF:response.setHeader(“内容类型”,“application/pdf”);response.setHeader(“Content Disposition”、“attachment;filename=“+filename”);org.apache.commons.io.IOUtils.copy(inStream,response.getOutputStream());response.getOutputStream().flush();inStream.close();但第一个问题是:我无法轻松获取内容类型(唯一的想法是:打开文件名结尾,但是,嗯…)第二个问题:文件名现在是一些神秘的id,尽管文件可以下载并打开。