download files
This commit is contained in:
@@ -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"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user