0

I have a webpage that takes time to load (a lot of data to get). In this webpage, I have a table with all my data.

I would like to be able to click on a button, and javascript will get the content of my table ($("#mytableId").html(); with jQuery) and let the user download it.

That would avoid me to reload the page (and all the data processing) but I have no idea how I could do this or even if this is possible. (And I don't think that I use the correct search terms in Google...)

Is there a way to do this ?

j08691
  • 204,283
  • 31
  • 260
  • 272
user3017110
  • 155
  • 12
  • Yes, if your server can serve JSON (or directly HTML data) you can make an asynchronous request in jQuery with $.ajax(). Of course your server has to provide only required table and not whole page so you have to write proper server side code for that (you don't say what you're using, PHP/JSP/ASP.NET/something else,) – Adriano Repetti Jan 28 '14 at 12:00
  • `and let the user download it.` - what do you mean by `download`? – Nick R Jan 28 '14 at 12:01
  • Nick R : I want the user to have a "pop up" that ask if the user wants to open or download the file. Adriano : My issue is that I don't want to use ajax ! I want my javascript code to get a part of my webpage and serve it to the user in a file. Without having to reload anything (getting all the data takes time sever side) – user3017110 Jan 28 '14 at 13:31

1 Answers1

0

Finally, I found the way to do what I wanted :

<a onclick="$(this).attr('href', 'data:text/plain;base64,' + utf8_to_b64($('#myTable')));" download="index.xls" id="generate">Generate static</a>

with this :

function utf8_to_b64( str ) {
  return window.btoa(unescape(encodeURIComponent( str )));
}

I don't need to reload the page to be able to download the data.

The complete link from stackoverflow is here: How to download the current page as a file / attachment using Javascript?

Community
  • 1
  • 1
user3017110
  • 155
  • 12