Я подключил следующий код из разных блогов и ТАК, так как не смог найти учебник для этого.
Что мне нужно сделать, это загрузить файл из REST API с использованием angular и typcript,У меня нет доступа к API (только URL, который похож на "http: // ..." и ничего более.
Вот мой машинописный код:
//Downloading File
// may be located in any component
let options = {
responseType: ResponseContentType.Blob //This has compilation error. Cannot recognize ResponseContentType.Blob
}
// pass options to request
this.http.get(url, options)
let type = "my/mime";
// download the file - retrieve observable
this.http.get(url, options).map((res: Body) => new Blob([res.blob()], { type: type }));
public downloadAttachment(attachment: MediaDownloadFileDto): void {
this.myDownloadService.downloadFile(downloadUrl, mimeType).subscribe((fileBlob: Blob) => {
fileSaver.saveAs(fileBlob, attachment.filename);
}, (message: string) => {
// error handling...
});
}
// may be located in any service:
public downloadFile(url: string, type: string): Observable<Blob> {
let options = {
responseType: ResponseContentType.Blob
};
return this.http.get(url, options).map((res: Body) => new Blob([res.blob()], { type: type }));
}
Вот HTML-кнопка, которую я использую:
<button class="nui-button nui-button--alt download" (click)="downloadFile(url,options)">Download Template</button>
Спасибо