Download coming table of AJAX request return

Asked

Viewed 68 times

2

Test scenario

I have a page with input "start date" and "end date", and a "spreadsheet" button".

The button calls the function planilha().

Function planilha():

function planilha() {

    var dados = $('#meuform').serialize();
    arquivo = "planilha.php";
    $.ajax({
        type:'post',
        data:dados,
        url:arquivo,
        success: function(retorno){      
            ??????????
        },
        error: function(){
            alert('Falha');
        }
    });
}

The archive planilha.php is being generated perfectly, and returning a ready-made table.

This same file, I already file spreadsheet and force the download, using:

header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=\"{$arquivo}\"" );

But now I’d like to do it right through AJAX.


Doubt

I would like to download this return in format xls.

  • Is there any way to do that or will I have to generate the table for JS to get it down?
  • take a look at this example, https://answall.com/questions/131627/download-archival-via-ajax?rq=1

  • @Lodi I did not find anything that would help me on this link. One answer uses the document and not the return. And the other uses XMLHttpRequest. As I know very little about JS I may have been mistaken.

  • "You can use a Javascript plugin called Download.js, but it is not compatible with previous versions of Internet Explorer, only from 9."Words of Uilque Messias follows the link to analyze http://danml.com/download.html You can use the library or build your own, if you take into account that its source is at home!

  • @Lodi But he is using downloading content from the page, and not from the return of the ajax. But I will take a look yes in this library. But I would also like an answer from you, so it would help a lot. Thanks!

  • I have never downloaded through ajax, I do not have a very precise answer, but I believe that you giving a Return in the file, should already give to try to download using this library!

  • @Lodi Could you give me an example of this return? The library even has the ajax fallback, but it looks like it’s only for html file download. Then you would have to see a change...

  • i would try so, in php function, you return the file loaded in a variable or a URL for it to download the generated file, and in Success you either open a link with the download using _Blank for new page to do with jquery, or use this library to capture the file in bits, and then save it to the pc?

  • var url = $(this). attr('href'); window.open(url, '_Blank');

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.