Download file through an ajax call php

后端 未结 5 1271
别跟我提以往
别跟我提以往 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:01

    You can't download the file directly via ajax.

    You can put a link on the page with the URL to your file (returned from the ajax call) or another way is to use a hidden iframe and set the URL of the source of that iframe dynamically. This way you can download the file without refreshing the page.

    Here is the code

    $.ajax({
        url : "yourURL.php",
        type : "GET",
        success : function(data) {
            $("#iframeID").attr('src', 'downloadFileURL');
        }
    });
    

提交回复
热议问题