Download File Through an AJAX Call in PHP



Using Ajax to download files is not considered to be a good idea. Instead, window.location = or document.location should be used.

The 'window.location' has the following characteristics −

  • JavaScript enabling is required
  • It doesn't need PHP.
  • It helps show the content of the site, and redirects the user after a few seconds.

Redirect can be dependent on any conditions such as −

$success = 1
if ($success) {
   window.location.href = 'http://example.com';
}

A variable named ‘success’ is assigned with the value of 1. When this condition is satisfied, the window.location is used.

On running, the user is redirected to the website 'http://example.com'

Updated on: 2020-04-07T11:40:45+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements