-4

I'm using Angular 6 for my project, in my index page I'm binding data and submit it to db, I want to submit whole html page with bonded data to db as string.

Can I know how to convert html page to base 64 string?

Wasif Ali
  • 886
  • 1
  • 13
  • 29
Mahesh Sanjeewa
  • 217
  • 2
  • 11
  • 1
    See how to convert string to base64 https://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript then you make your whole html as one string then convert it. – holydragon Feb 13 '19 at 07:43

2 Answers2

3

You can get you Markup as a string with,

var markup = document.documentElement.innerHTML;

And then convert that string to base64 using btoa() like this,

var encoded = window.btoa(markup);

Or if you get UTF8, use this,

var encoded = window.btoa(unescape(encodeURIComponent(markup)));
Wasif Ali
  • 886
  • 1
  • 13
  • 29
  • 1
    its working but when i put big contain indo btoa(); the console get this error, can i know how to fix this Uncaught (in promise): InvalidCharacterError: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range. Error: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range. – Mahesh Sanjeewa Feb 13 '19 at 08:55
  • 2
    `btoa(unescape(encodeURIComponent(markup)))` I believe this will solve your issue. – Wasif Ali Feb 13 '19 at 08:58
  • 1
    Pleasure, you can accept the answer as well by clicking the tick icon :P – Wasif Ali Feb 13 '19 at 09:22
2

You can get the HTML as string of any website with

window.document.documentElement.innerHTML
Stefan
  • 14,826
  • 17
  • 80
  • 143