0

I have a code which converts JSON to CSV. And I put this through a specific condition ( if condition) I want the CSV to be downloaded automatically if the condition is met without any button click. what is the shortest/simplest way to do this?

For eg:

if( data.length == 10){
        var stored_data = data;
        data = [];

        console.log(stored_data);
        var csv_file = ConvertToCSV(stored_data);   //ConvertTOCSV is a function

        }

if the above condition is met, the CSV should be downloaded. Thank you

NOTE: I know this can e done easily with a button click. but i want the download to happen automatically when the condition is satisfied

Sandy.Arv
  • 607
  • 10
  • 34
  • 1
    why did you use || condition there? you could have used '>=' greater or equal to.. this could be more than enough.. – ameenulla0007 Feb 17 '16 at 16:32
  • 1
    Possible duplicate of [How to export JavaScript array info to csv (on client side)?](http://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side) – Popnoodles Feb 17 '16 at 16:35
  • @ameenulla0007: that was so silly of me.. – Sandy.Arv Feb 17 '16 at 16:38
  • @Popnoodles I am sorry, but how is this a duplicate? I clearly know how to do this with a button click. I am asking for a way to do it without a button click. – Sandy.Arv Feb 17 '16 at 16:40
  • Not exactly related, but you loose the content of `data`. Setting `stored_data = data` doesn't create a copy of `data`, it just sets a reference to the same object. Then when you set `data = []`, all the previous content is gone, from both variables. Related: If yo know how to do this with a button click, what prevents you to use the same code inside the `if` block? But when you want to execute the if? – Teemu Feb 17 '16 at 16:53
  • @Teemu actually, on a bigger picture, im parsing a dynamic website, which updates its data every half an hour, i have programmed my code to reload every half an hour and stored the data in local storage,`stored_data = data` is nothing but an array which stores all the data from the local storage. so when the length of the array reaches 10, i need to download the CSV. Since the program runs continuously, i wont be available to trigger a download, so it should happen automatically whenever the condition is met – Sandy.Arv Feb 17 '16 at 17:08
  • @Sandy.Arv if you read the answer it tells you how to do it without the user clicking- what you asked for. – Popnoodles Feb 19 '16 at 13:05

2 Answers2

2

Just create a "dummy" link and programmatically set its href and then click. The link shouldn't appear on the screen because it has no content.

<body>
  <a href="#" id="csvDownload" download></a>
</body>
<script>
if( data.length == 10){
        var lnk = document.getElementById("csvDownload");
        var stored_data = data;
        data = [];

        console.log(stored_data);
        var csv_file = ConvertToCSV(stored_data);   //ConvertTOCSV is a function
        lnk.href = csv_file;
        lnk.click();
 }
</script>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

What you are trying to do is possible by changing the document location to point to your created object, but the problem that I ran into while trying to create a quick example for you, is I can't seem to give the file a usable name and extension. So everything downloaded as a file called "download" with no extension (however - the file contents are correct).

So if you want to pursue finding a way to give the stream a name and file type you could start with something like this -

document.location='data:Application/octet-stream,' + encodeURIComponent(csv_file);

But I would say that @Scott's answer is a better way to go.

hardba11
  • 1,478
  • 15
  • 27