Crazylife's avatar

How to download a zip file using ajax?

I have written this statement to download zip file from my backend.

 return response()->download($filetopath, $zipFileName, $headers)->deleteFileAfterSend(true);

It works well without ajax, but how it works if i want to use ajax to download?

success: function (response) {
				var a = document.createElement("a");
				a.href = response.file;
				a.download = response.name;
				document.body.appendChild(a);
				a.click();
				a.remove();
			},

It shows undefined to me

0 likes
1 reply
TutanRamon's avatar

Well, what I often do I as follows:

in your controller just simply return the filepath and name in the Ajax succes response and use something like below:

$('#downloadZip').click(function(){
   $.ajax({
      url: 'yourControllerRoute.php',
      type: 'post',
      success: function(response){
         window.location = response;
 }

}); }); });

Please or to participate in this conversation.