Why don't you use a normal href to download the file?
May 8, 2022
15
Level 2
Safari force pdf file download
Hello, I have an app laravel + vuejs in which users can download offer pdf file like this:
public function show($id): JsonResponse|BinaryFileResponse
{
$offer = (Offer::findOrFail($id)) ?? NULL;
$file_path = '/offers/' . $offer->pdf_file;
if(!Storage::disk('local')->exists($file_path)) {
return response()->json(['error' => 'File not found'], 404);
} else {
return response()->file(storage_path('/app/') . $file_path, headers: ['Content-Type' => 'application/pdf']);
}
}
in vuejs I fetch it like:
const response = await axios.get(`/api/offer/${id}`, {responseType: 'arraybuffer'});
let blob = new Blob([response.data], {type: 'application/pdf'});
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
window.open(link.href, '_blank');
This works fine in Chrome, it opens selected offer in new tab or download it on mobile. In Safari on iPhone nothing happens when I click the link. Now I saw online there are some solutions but mostly for file download, but I want it to open in new tab (at least on desktop). This might work but it will download file:
link.download = 'myfile.pdf';
link.click();
This is probably more like question for vuejs forum but maybe there is something I can do on laravel part to force the file download.
Thx!
Please or to participate in this conversation.