Arrays 从字节[]下载PDF

Arrays 从字节[]下载PDF,arrays,.net,angular,file,Arrays,.net,Angular,File,服务器端(Visual Studio 2015) 我有一个pdf存储在我的服务器端。通过WebApi从客户端调用时,我使用以下指令读取并存储变量的内容: byte[] bytes = File.ReadAllBytes(path); return Request.CreateResponse(HttpStatusCode.OK, bytes); 然后,我按照以下说明将文件内容发送到客户端: byte[] bytes = File.ReadAllBytes(path); return Req

服务器端(Visual Studio 2015)

我有一个pdf存储在我的服务器端。通过WebApi从客户端调用时,我使用以下指令读取并存储变量的内容:

byte[] bytes = File.ReadAllBytes(path);
return Request.CreateResponse(HttpStatusCode.OK, bytes);
然后,我按照以下说明将文件内容发送到客户端:

byte[] bytes = File.ReadAllBytes(path);
return Request.CreateResponse(HttpStatusCode.OK, bytes);
客户端(角度7) 安装了FileSaver后,我收到了文件内容,看起来是这样的

JVBERI0xLJNCIW1TBW1DQOXIDAGB2JQ…eXBlL0NhdGFsb2cvUG

然后我尝试通过以下方式下载它:

this.fileService.fileDownload().subscribe(bytes => {
    const file = new Blob([bytes], {type: 'application/pdf'});
    saveAs(file, 'file.pdf');
}
使用fileService中的以下方法调用服务器API:

fileDownload(id): Observable<HttpResponse<any>>{
    const httpOptions = {
      'responseType'  : 'arraybuffer' as 'json'
    };
    return this.http.get<any>('http://localhost:55820/api/files/12',httpOptions);
  }
fileDownload(id):可观察{
常量httpOptions={
'responseType':'arraybuffer'作为'json'
};
返回此.http.get('http://localhost:55820/api/files/12,httpOptions);
}
但该文件已损坏或Acrobat Reader不接受


我还希望它适用于其他文件类型,具有
字节[]
中的文件内容、内容类型定义(text/plain、application/pdf)和带扩展名的文件名。我如何使这个过程标准化

httpOptions应该是
{responseType:'blob'}

试着这样做:

fileDownload(id): Observable<HttpResponse<any>>{
    return this.http.get('http://localhost:55820/api/files/12',{ responseType: 'blob' });
}



this.fileService.fileDownload().subscribe(bytes => {
    saveAs(file, 'file.pdf');
}
fileDownload(id):可观察{
返回此.http.get('http://localhost:55820/api/files/12“,{responseType:'blob'});
}
this.fileService.fileDownload().subscribe(字节=>{
saveAs(文件“file.pdf”);
}

响应类型应类似于responseType:“blob”,从observable获取blob响应后,您可以在downloadFile(content)函数中传递blob响应,我们将在该函数中给出文件类型(如pdf、zip等)和文件名,并创建要下载的事件

download(id): Observable<Blob> {
    return this.http.get(this.downloadUrl + id 
    { 
        headers: this.getHeaders(), responseType: 'blob' })
        .pipe(catchError(this.handleError<any>('download')));
    }

this.downloadService.download(id).subscribe(data => {
    this.downloadFile(data);
});

downloadFile(content) {
    const fileName = 'dowmloadfile.pdf';
    const type = 'pdf';
    const e = document.createEvent('MouseEvents');
    const a = document.createElement('a');
    a.download = fileName;
    a.href = window.URL.createObjectURL(content);
    a.dataset.downloadurl = [type, a.download, a.href].join(':');
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
}
下载(id):可观察{
返回this.http.get(this.downloadUrl+id
{ 
headers:this.getHeaders(),responseType:'blob'})
.pipe(catchError(this.handleError('download'));
}
this.downloadService.download(id).subscribe(数据=>{
下载文件(数据);
});
下载文件(内容){
常量文件名='dowmloadfile.pdf';
常量类型='pdf';
const e=document.createEvent('MouseEvents');
常量a=document.createElement('a');
a、 下载=文件名;
a、 href=window.URL.createObjectURL(内容);
a、 dataset.downloadurl=[type,a.download,a.href].join(“:”);
e、 initMouseEvent('click',true,false,window,0,0,0,false,false,false,0,null);
a、 调度事件(e);
}

使用该方法获取此错误:
类型“blob”不可分配给类型“json”.[2322]
尝试
{observe:'response',responseType:'blob'}
获取的
类型“response”不可分配给类型“body”
并且
类型“blob”不可分配给类型“json”“
Remove
。您将不会再收到此错误,很抱歉,我没有注意到
任何
早期尝试此{responseType:'blob'作为'json'}”