PHP download files from the server does not work with ajax

0

I have the following problem, I hope you can help me: I have a php script to download files from the server what works correctly if I call it from a href tag. However if I call the same php script with ajax it does not work. (The issue of doing it with ajax is to pass the parameter of the path of the file to download as the user decides which file to download). Below I share the summary code:

PHP (mydownload.php)
<?php
header("Content-disposition: attachment; filename=myfile.pdf");
header("Content-type: application/pdf");
readfile("myfile.pdf");
?>

HTML (index.html)
<head>
<script type="text/javascript" src="js/jquery/jquery-3.1.1.min.js"></script>

<script type="text/javascript">
function miFuncion()
{
 $.ajax({
  url: "mydownload.php",
  type: "post",
  success: function(res){
  }
 })
}
</script>

</head>
<body>
<a href="mydownload.php">Download the document (PDF)</a> // Funciona, pero solo baja el archivo previamente definido en el script php

<form>
 <input type="button" onclick="miFuncion()" value="Activar Función DL"> // No Funciona!
</form>

</body>
</html>

Actually what happens when calling the php script with ajax is that instead of downloading the file it returns the following screen:

I really appreciate any help with this, or if there is another method, other than ajax, to call the php script that allows you to pass the parameter of the file that you want to download, it would be equally useful. Thank you very much in advance

    
asked by Mario Zanetta 19.07.2017 в 19:18
source

0 answers