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"> <td mat-cell *matCellDef="let element">
<button <button
mat-icon-button mat-icon-button
(click)="onClickDownload(element.ID)" (click)="onClickDownload(element.ID, element.Name)"
matTooltip="Datei herunterladen" matTooltip="Datei herunterladen"
matTooltipPosition="left" 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) { public onFileOverNew(hover: boolean) {
this.fileOverNew = hover; this.fileOverNew = hover;
} }

View File

@@ -2,28 +2,25 @@ import { base } from './urls';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class DownloadService { export class DownloadService {
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient) {}
public get(id: number, withKey: boolean) { public get(songId: number, fileId: number, filename: string) {
return this.httpClient return this.httpClient
.get(base + '/' + id + '?withKey=' + withKey, { .get(base + '/api/songs/' + songId + '/files/' + fileId, {
responseType: 'blob' as 'json' responseType: 'blob' as 'json',
observe: 'response'
}) })
.subscribe( .subscribe(
(response: any) => { (response: any) => {
const dataType = response.type; const contentType = response.headers.get('Content-Type');
const binaryData = [];
binaryData.push(response);
const downloadLink = document.createElement('a'); const downloadLink = document.createElement('a');
downloadLink.href = window.URL.createObjectURL( const blob = new Blob([response.body], { type: contentType });
new Blob(binaryData) downloadLink.href = window.URL.createObjectURL(blob);
); downloadLink.setAttribute('download', filename);
downloadLink.setAttribute('download', id + '.doc');
document.body.appendChild(downloadLink); document.body.appendChild(downloadLink);
downloadLink.click(); downloadLink.click();
}, },