Skip to content
Permalink
Browse files
feat(archive-value): Download archive files with original filename (D…
…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 a87dfae3b0f427ed58442221c724e3daab83820d
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';
@@ -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) {
@@ -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);

0 comments on commit a87dfae

Please sign in to comment.