Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream AJAX output to a file download in Javascript?

We have an REST API emitting JSON from where we can source data (using AJAX). What we want to do is to be able to do AJAX calls where each call fetches a small chunk of data (say 5000 rows or so), and then have it written out to a CSV file on the browser as a download.

It seems that if we have all the data in JS memory, then writing it out to CSV file is not hard, but if we want to write out 100K records, then we are forced to fetch all 100K in one shot and then produce the file in one go.

Instead, we feel it would be much gentler on both server and client to download small chunks and stream it out to the file download. Is there a way to do it?

(We are presently using jQuery 2.0.0, but don't mind using a different library to get this done)

like image 756
Shreeni Avatar asked Jun 29 '15 06:06

Shreeni


1 Answers

Basically you are looking for paging of record...for this you can do

  1. Find total number of records in database
  2. Than divide your records/no of calls
  3. Merge all the data coming from different calls

for making multiple call using jquery you can do like this

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
  // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
  // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
  var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
  if ( /Whip It/.test( data ) ) {
    alert( "We got what we came for!" );
  }
});

in code there are multiple ajax call and at last it merging data ...that same thing on server side you have to do if you are using C# than TPL (Task parellel library) is good option...for each call you need to call with the page number

like image 169
Pranay Rana Avatar answered Sep 29 '22 04:09

Pranay Rana