I have a several pdf file displayed in my website and i created a function that if i will click it will automatically download instead of opening it in a new window. I can download it but the problem is if i open it, it is empty but if i open it in another editor like notepad. It got texts like
�o�w��v�����%l&��䰱�L'ā5���
What should I do about this? I did download the file right? The data seems encrypted. How could i decrypt it and show in pdf?
Here is my axios function
downloadWithAxios(file){
axios.get(encodeURI(file.url,{
method: 'GET',
mode: 'no-cors',
responseType: 'blob',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/pdf',
},
}))
.then((response)=>{
console.log(response.data)
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
link.click();
})
}
You actually downloaded the file but I think you must need to decode it. Actually you can try back-end approach by doing
Make an axios calling a url route and pointing to the controller
downloadFromController(file){
let params = {url : file.url}
console.log(params);
axios.get('/generateFile?params='+file.url)
.then(()=>{
window.location.assign('/generateFile?params=' +file.url); //prevent from redirecting to pdf url
})
}
Your route pointing to your controller: Route::get('/generateFile{params?}','YourController@downloadFile');
And in your controller define the function like
public function downloadFile(Request $request){
$url = $request->params; //pass the url params from your axios
$name = 'file.pdf'; //your file name
$temp = tempnam(sys_get_temp_dir(), $name);
copy($url, $temp); //use copy() then send it to user in the response
return response()->download($temp,$name);
}
It will not open a new window. It automatically downloads if you click the link of your pdf.