1

I am trying to generate an Excel file and downloads in the browser when user clicks a button.

   var vFullExportContent = getExcelXmlNew(includeHidden, fullPageStore);   

This generated the Excel file data with XML.

and i wanted to encode it using Base64 and download the file using document.location using below line of code. And this does not generate a file when i have my excel records more than 1000 rows. below 1000 rows it is ok.

    document.location='data:application/vnd.ms-excel;base64,' + Base64.encode(vFullExportContent);

And also i verified all of my (3259 rows) data for any encoding issues, doesn't observed anything. that is i applied filters in application to retrieve unique rows below thousand and all the three times report downloads.

Help is much appreciated.

Kiran
  • 11
  • 2
  • Your approach generates a `data:` `URI` which contains all data. So this `URI` will be huge if the data is huge and so it can hit some [maximum-length-of-a-url-limit](http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers). So what browser are you using and have you tried with Firefox browser? In general this approach should be avoided and a library should be used which can really create `Excel` files which then can be downloaded. – Axel Richter Mar 26 '17 at 07:57

1 Answers1

1

You no longer need Base64 nowadays. Instead, you can wrap your XML in a Blob and give the user an Object URL to download it.

First, blobs are blocks of binary data, not JavaScript text, so we convert your text to a Uint8Array (you may have to polyfill TextEncoder in your browser):

var encoder = new TextEncoder('utf8')
var blob = new Blob(encoder.encode(vFullExportContent), {type: "application/xml"})

next, generate the URL (it will be a short URL that references the Blob, not a representation of the data):

var url = URL.createObjectURL(blob)

and show a link to that (or use window.location if you must).

Once the user has downloaded the file, don't forget to call URL.revokeObjectURL(blob) so that the blob can be garbage collected.

Touffy
  • 6,309
  • 22
  • 28