Skip to content

Commit

Permalink
feat(archive-value): Download archive files with original filename (D…
Browse files Browse the repository at this point in the history
…EV-295) (#677)

* feat(archive-value): download file with original filename

* feat(archive): sets the filename
  • Loading branch information
mdelez committed Mar 2, 2022
1 parent d3a1e49 commit a87dfae
Showing 1 changed file with 25 additions and 3 deletions.
@@ -1,4 +1,4 @@
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Component, Input, OnInit } from '@angular/core';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
import { FileRepresentation } from '../file-representation';
Expand All @@ -11,13 +11,28 @@ import { FileRepresentation } from '../file-representation';
export class ArchiveComponent implements OnInit {

@Input() src: FileRepresentation;
originalFilename: string;
temp: string;

constructor(
private readonly _http: HttpClient,
private _errorHandler: ErrorHandlerService
) { }

ngOnInit(): void { }
ngOnInit(): void {
const requestOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true
};

const pathToJson = this.src.fileValue.fileUrl.substring(0, this.src.fileValue.fileUrl.lastIndexOf('/')) + '/knora.json';

this._http.get(pathToJson, requestOptions).subscribe(
res => {
this.originalFilename = res['originalFilename'];
}
);
}

// https://stackoverflow.com/questions/66986983/angular-10-download-file-from-firebase-link-without-opening-into-new-tab
async downloadArchive(url: string) {
Expand All @@ -33,7 +48,14 @@ export class ArchiveComponent implements OnInit {
const url = window.URL.createObjectURL(data);
const e = document.createElement('a');
e.href = url;
e.download = url.substr(url.lastIndexOf('/') + 1);

// set filename
if (this.originalFilename === undefined) {
e.download = url.substr(url.lastIndexOf('/') + 1);
} else {
e.download = this.originalFilename;
}

document.body.appendChild(e);
e.click();
document.body.removeChild(e);
Expand Down

0 comments on commit a87dfae

Please sign in to comment.