download files
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
<td mat-cell *matCellDef="let element">
|
||||
<button
|
||||
mat-icon-button
|
||||
(click)="onClickDownload(element.ID)"
|
||||
(click)="onClickDownload(element.ID, element.Name)"
|
||||
matTooltip="Datei herunterladen"
|
||||
matTooltipPosition="left"
|
||||
>
|
||||
|
||||
@@ -43,7 +43,9 @@ export class SongFilesComponent {
|
||||
});
|
||||
}
|
||||
|
||||
public onClickDownload(id: number): void {}
|
||||
public onClickDownload(fileId: number, filename): void {
|
||||
this.downloadService.get(this.selectedSongId, fileId, filename);
|
||||
}
|
||||
public onFileOverNew(hover: boolean) {
|
||||
this.fileOverNew = hover;
|
||||
}
|
||||
|
||||
@@ -2,28 +2,25 @@ import { base } from './urls';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DownloadService {
|
||||
constructor(private httpClient: HttpClient) {}
|
||||
|
||||
public get(id: number, withKey: boolean) {
|
||||
public get(songId: number, fileId: number, filename: string) {
|
||||
return this.httpClient
|
||||
.get(base + '/' + id + '?withKey=' + withKey, {
|
||||
responseType: 'blob' as 'json'
|
||||
.get(base + '/api/songs/' + songId + '/files/' + fileId, {
|
||||
responseType: 'blob' as 'json',
|
||||
observe: 'response'
|
||||
})
|
||||
.subscribe(
|
||||
(response: any) => {
|
||||
const dataType = response.type;
|
||||
const binaryData = [];
|
||||
binaryData.push(response);
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
const downloadLink = document.createElement('a');
|
||||
downloadLink.href = window.URL.createObjectURL(
|
||||
new Blob(binaryData)
|
||||
);
|
||||
downloadLink.setAttribute('download', id + '.doc');
|
||||
const blob = new Blob([response.body], { type: contentType });
|
||||
downloadLink.href = window.URL.createObjectURL(blob);
|
||||
downloadLink.setAttribute('download', filename);
|
||||
document.body.appendChild(downloadLink);
|
||||
downloadLink.click();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user