Download file through an AJAX call in PHP

PHPServer Side ProgrammingProgramming

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 = '';

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 ''

Updated on 07-Apr-2020 11:40:45