I already uploaded a file and saved the path into my database, but the problem now is that I can't download it. I have already tested the PHP code and it works, but when I try to download it from the Angular side it doesn't. Instead I get this error in the console:
httpErrorResponse error: SyntaxError: Unexpected token p in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.onLoad (localhost:4200/vendor.js:164415:51)
at ZoneDelegate.invokeTask (localhost:4200/polyfills.js:9799:31)
.........
ANGULAR SERVICE:
public getjointes(filepath:string){
const params = new HttpParams()
.set('filepath', filepath);
return this.http.get(url+'/download.php', {params: params });
}
TYPESCRIPT / function
telecharger(path:string) {
console.log(path);
this.stream.getjointes(path).subscribe(response => {
console.log(response);
});
}
PHP CODE:
$rest = $_GET['filepath'] ;
echo($rest);
$filepath = 'upload/' . $rest;
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('upload/' . $rest));
readfile('upload/' . $rest);
header("Access-Control-Expose-Headers: Content-Disposition");
}
else{
echo ('noo');
}