Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

khaledz's avatar

Forcing downloads is not working in all browsers.

Hello,

I'm building application using Lumen to fetch external url and force download it using VueJS. The code works fine in Chrome but it asks me whether to download the files or not and I click 'Download;. However, I've tested the code in firefox and safari and it's not working and there is no dialog to enable downloading files.

here is my code: .php

header("Content-Description: File Transfer"); 
        header("Content-Type: application/octet-stream"); 
        header('Content-Disposition: attachment; filename="'. $request->title . '.' . $request->type .'"');
        header('Content-Length: ' . $filesize);
        readfile($request->url);

.vue

let blob = new Blob([response.data], { type: 'audio/m4a' })
                            let link = document.createElement('a');
                            link.href = window.URL.createObjectURL(blob);
                            link.download = data.title + '.m4a';
                            link.click();
                                

My question is, how to make sure that the above code will work fine in all browsers? Thank you

0 likes
2 replies
Braunson's avatar

Have you tried using the Lumen download method? https://lumen.laravel.com/docs/5.2/responses#file-downloads

response()->download($pathToFile)

The download method may be used to generate a response that forces the user's browser to download the file at the given path. The download method accepts a file name as the second argument to the method, which will determine the file name that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method:

I've had issues with certain browsers downloading files depending on the headers set.

1 like
khaledz's avatar

Hi @braunson

In my situation the url is external so I think response()->download($pathToFile) will not be suitable for this.

My code works fine in Chrome & Firefox but not in IOS, and I've not tested in Mac yet.

thanks

1 like

Please or to participate in this conversation.