download files

This commit is contained in:
Benjamin Ifland
2019-03-28 12:51:32 +01:00
parent 06ef90fce4
commit 787dba4c0e
3 changed files with 12 additions and 13 deletions

View File

@@ -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"
>

View File

@@ -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;
}

View File

@@ -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();
},