简体   繁体   English

如何在Angular 7中使用Blob下载任何文件类型(dgn,docx等)

[英]How to download any file types(dgn,docx etc) using blob in Angular 7

Am returning byte Array from API. 我正在从API返回字节数组。 My requirement is to download all file types .Getting error(Failed to load PDF document) while downloading dgn files or other types 我的要求是下载所有文件类型。下载dgn文件或其他类型时出现错误(无法加载PDF文档)

Am able to convert byte array to blob and downloading PDF files . 能够将字节数组转换为blob并下载PDF文件。

 download(): void {
        this._service.download().subscribe(data => {
            this.downloadFile(this.byteToBlob(data, 'application/octet-stream', 512));
        },
            error => console.log("Error downloading the file."),
            () => console.log('Completed file download.'));
    }


    downloadFile(data: any): void {
        const blob = new Blob([data], { type: 'application/pdf' });
        const url = window.URL.createObjectURL(blob);
        window.open(url);
    }

Getting error "Failed to load PDF document " while downloading non PDF files 下载非PDF文件时出现错误“无法加载PDF文档”

Hope your backend should like as follows 希望您的后端如下

let file = fs.readFileSync(tmpFilePath)
res.setHeader('Content-disposition', `attachment; filename=temp_file.jpg;status=OK`)
res.send(file)

Angular Service 角度服务

downloadMedia(): Observable<any> {
return this.http.get<any>(`/api/event/medias/download/image`, {responseType: 'blob' as 'json'})

} }

Component 零件

this.service.downloadMedia().subscribe(result => {
    let imageFormat = result.type.toString()
    saveAs(result, 'temp_file.jpg');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM