Helper.js 1.3 KB

1234567891011121314151617181920212223242526
  1. "use strict";
  2. function clickElement(element) {
  3. element.click();
  4. }
  5. function downloadFromUrl(options) {
  6. var _a;
  7. var anchorElement = document.createElement('a');
  8. anchorElement.href = options.url;
  9. anchorElement.download = (_a = options.fileName) !== null && _a !== void 0 ? _a : '';
  10. anchorElement.click();
  11. anchorElement.remove();
  12. }
  13. function downloadFromByteArray(options) {
  14. var url = typeof (options.byteArray) === 'string' ?
  15. // .NET 5 or earlier, the byte array in .NET is encoded to base64 string when it passes to JavaScript.
  16. // In that case, that base64 encoded string can be pass to the browser as a "data URL" directly.
  17. "data:" + options.contentType + ";base64," + options.byteArray :
  18. // .NET 6 or later, the byte array in .NET is passed to JavaScript as an UInt8Array efficiently.
  19. // - https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/6.0/byte-array-interop
  20. // In that case, the UInt8Array can be converted to an object URL and passed to the browser.
  21. // But don't forget to revoke the object URL when it is no longer needed.
  22. URL.createObjectURL(new Blob([options.byteArray], { type: options.contentType }));
  23. downloadFromUrl({ url: url, fileName: options.fileName });
  24. if (typeof (options.byteArray) !== 'string')
  25. URL.revokeObjectURL(url);
  26. }