I'm writing a web-app with angular frontend and php backend in angular facing the challenge now, when trying to download a file it is loaded completely by the following script.
Angular service:
downloadFile(f:string){
this.http.post('/api/download.php', JSON.stringify({"file": f}), {responseType:'arraybuffer'})
.subscribe(response => {
console.log(response);
});
}
Used imports:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
The PHP file looks like this (works fine):
include "config/var.inc.php";
$dir = "../" . DATA_FOLDER . "/";
$data = json_decode(file_get_contents("php://input"));
$file = $dir . $data->file;
if(file_exists($file)){
header('Content-Description: download audio');
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
The console.log
shows me the file is completely loaded (23300766 Bytes)
How can I deliver it to the user as a download file?