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

cooperino's avatar

How to download a file from an ajax call?

This is another attempt and trying to download a file using a download link.

I now try to call my controller's download method via jQuery ajax:

$.ajax({
    url: downloadUrl,
    type: "GET",
    }).done(function() {
        alert("done");
    });

I tried the following two ways to return a download response to the ajax:

return Response::make(Storage::disk('s3')->get($request->path), '200', $headers);

or

return Storage::disk('s3')->download($request->path);

both of them just show up as a text in the response in the browser, but no download is happening

I actually see the data in the response in the Network tab. But how can I trigger a download?

0 likes
11 replies
Nakov's avatar

Why do you need to call it with AJAX?

You can just do this:

window.location = downloadUrl;
1 like
cooperino's avatar

@Nakov Because I want to redirect to another page when hitting the download endpoint. Is it possible using window.location? I tried to do:

window.location = downloadUrl;
window.location = newUrl; (or with window.location.replace)

But then the download doesn't start

Nakov's avatar

@cooperino You can't do those one after the other.

You can redirect to another page from your download method in the controller.

1 like
cooperino's avatar

@Nakov How? Because right now I download with return. Didn't find a way to do it without:

(For example return Storage::disk('s3')->download($request->path);)

Nakov's avatar

@cooperino try this with just JavaScript:

window.open(downloadUrl, '_blank'); // open it in new tab, which will automatically close
window.location = newUrl;
1 like
cooperino's avatar

@Nakov Yes currently that's my workaround (exactly this post). It's working, but not sure how good of a practice it is to mimic a user's click, it feels like it's sort of malware behavior. Hopefully my website won't be blocked by some browser protection extensions if I use that

Nakov's avatar

@cooperino typically what I do with Download is just a Download, so no redirect or anything. Because a Download action at least to me sounds like I want to Download something, and not be redirected to somewhere else afterwards, at least that's not what I would expect as a user.

1 like
cooperino's avatar

@Nakov I would also just download, but there is a bug with opening links from email. I've tried all Laravel's downloading ways: with make, with download, with streamDownload - they all download, but when opening the download link from the email it opens a blank tab until the page is refreshed

Nakov's avatar

@cooperino Is it just from the email or even if you try to access your link through the browser it also shows just a blank page?

You might need to add some headers to the response in order to let your browser know that the file should be downloaded.

Also if you are sending an email, why don't you attach the file in the email itself rather then a link to download?

Please or to participate in this conversation.