Download file through an ajax call php

后端 未结 5 1331
别跟我提以往
别跟我提以往 2020-11-22 14:51

I have a button and onclick it will call an ajax function.

Here is my ajax function

function csv(){

    ajaxRequest = ajax();//ajax()          


        
5条回答
  •  执笔经年
    2020-11-22 15:06

    @joe : Many thanks, this was a good heads up!

    I had a slightly harder problem: 1. sending an AJAX request with POST data, for the server to produce a ZIP file 2. getting a response back 3. download the ZIP file

    So that's how I did it (using JQuery to handle the AJAX request):

    1. Initial post request:

      var parameters = {
           pid     : "mypid",
         "files[]": ["file1.jpg","file2.jpg","file3.jpg"]
      }

      var options = { url: "request/url",//replace with your request url type: "POST",//replace with your request type data: parameters,//see above context: document.body,//replace with your contex success: function(data){ if (data) { if (data.path) { //Create an hidden iframe, with the 'src' attribute set to the created ZIP file. var dlif = $('