简体   繁体   中英

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

Am returning byte Array from API. My requirement is to download all file types .Getting error(Failed to load PDF document) while downloading dgn files or other types

Am able to convert byte array to blob and downloading PDF files .

 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

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');
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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