Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何从api保存文件_Angular_Api_File_Download - Fatal编程技术网

Angular 如何从api保存文件

Angular 如何从api保存文件,angular,api,file,download,Angular,Api,File,Download,我是angular方面的新手,我需要知道如何将API中的文件保存到用户计算机上。在我的API中,我在/download/chapter/{id}上有DownloadController和downloadChapter方法。当我从Chrome上调用这个方法时,我的电脑就开始下载章节文件了——这没关系。当我在angular中从我的DownloadService调用此方法时,我在控制台中得到错误:error OK 我的下载章节方法: downloadChapter(id: number): Obs

我是angular方面的新手,我需要知道如何将API中的文件保存到用户计算机上。在我的API中,我在/download/chapter/{id}上有DownloadController和
downloadChapter
方法。当我从Chrome上调用这个方法时,我的电脑就开始下载章节文件了——这没关系。当我在angular中从我的DownloadService调用此方法时,我在控制台中得到错误:error OK

我的下载章节方法:

  downloadChapter(id: number): Observable<Blob> {
    return this.http
      .get<Blob>(`${this.basicPath}/chapter/${id}`)
      .pipe(
        tap((res) => {
            console.log(`response: ${res}`);
          },
          (e) => {
            console.log(`error: ${e} - ???`);
          }, () => {
            console.log('win!');
          }),
      );
  }
下载章节(id:number):可观察{
返回此文件。http
.get(`this.basicPath}/chapter/${id}`)
.烟斗(
轻触((分辨率)=>{
log(`response:${res}`);
},
(e) =>{
log(`error:${e}-???`);
}, () => {
console.log('win!');
}),
);
}
在API回复中:

200好的,URL [文件:/Users/lukasz/Workspace/Inz/api/booky/uploads/1/6:Solaris%20-%20Maly%20Apokryf.mp3],[内容处置:“附件;文件名=6:Solaris%20-%20Maly%20Apokryf.mp3”,内容类型:“音频/mpeg”,内容长度:“97857000”]


如何修复它?我只想将文件保存在用户计算机上

下面是一个从restful api下载文件的示例:

安装文件保护程序:
npm安装文件保护程序--保存

import { HttpClient } from '@angular/common/http';
import { saveAs } from 'file-saver';

.
.
.
constructor(private http: HttpClient) { }

download() {
    this.http.get('/download/chapter/{id}', {responseType:'blob', observe: 'response'}).subscribe(
      (data : any)=>{
        saveAs(data.body, 'yourfilename.ext');
      },
      (err : HttpErrorResponse)=>{
        console.error('download failed:', err);
      });
}

.
.
.

查看
https://github.com/eligrey/FileSaver.js