Download.jslib 769 B

123456789101112131415161718192021222324
  1. mergeInto(LibraryManager.library, {
  2. Download: function (str, fn) {
  3. var msg = UTF8ToString(str);
  4. var fname = UTF8ToString(fn);
  5. function fixBinary(bin) {
  6. var length = bin.length;
  7. var buf = new ArrayBuffer(length);
  8. var arr = new Uint8Array(buf);
  9. for (var i = 0; i < length; i++) {
  10. arr[i] = bin.charCodeAt(i);
  11. }
  12. return buf;
  13. }
  14. var binary = fixBinary(atob(msg));
  15. var data = new Blob([binary]);
  16. var link = document.createElement('a');
  17. link.download = fname;
  18. link.href = URL.createObjectURL(data);
  19. document.body.appendChild(link);
  20. link.click();
  21. document.body.removeChild(link);
  22. }
  23. });