0

I am using Laravel 5.7 and I want to download zip files on request basis. I send the request to the server via Ajax (the following code), and it returns 200 OK, but does not download the zip file (I can see the content of the zip file in the console though). Also, when I make a regular request (using form to submit the request) it downloads the zip file (as expected), but not with the Ajax request. The reason which I want to use Ajax is to add loading spinner during the downloading the files. Am I missing anything here?

$.ajax({
       url: "my_url",
       type: 'POST',
       async: true,
       beforeSend: function() {
         $("#loading").show();
       },
       data: { selectedIds: ids },
       success: function(result) {
        //
       },
       complete:function(result) {
          $("#loading").hide();
       }
   });
Ali
  • 664
  • 3
  • 13
  • 21
  • Check if your CORS settings allow such request. – Vladislav Kryshtanovskiy May 11 '20 at 04:14
  • Does this answer your question? [Download zip file with jquery from ajax post request](https://stackoverflow.com/questions/23676748/download-zip-file-with-jquery-from-ajax-post-request) – Don't Panic May 11 '20 at 07:16
  • @Don'tPanic: No it doesn't. I also tried adding those lines, but didn't work. – Ali May 11 '20 at 16:28
  • I am not sure what lines you are referring to. If you search for "jquery AJAX download" there are many answers here on SO, almost all of them use the same approach as the one I already linked - in the AJAX success callback, redirect the browser to your file. There are also more modern approaches described. Here are some more examples. If these aren't suitable, edit your question and add some details explaining why. https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request, https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request – Don't Panic May 12 '20 at 23:50

0 Answers0