Monday, October 14, 2019

How do I name a file I made with the atob/Blob trick in JavaScript something other than a random Guid?

The last line here reading window.open(fileURL); needs to be replaced with seven new lines of code which look like this in JavaScript:

var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none;";
a.href = fileURL;
a.download = "HelloWorld.xlsx";
a.click();
window.URL.revokeObjectURL(fileURL);

 
 

In TypeScript instead of JavaScript the first of these lines changes up slightly like so:

var a = <any>document.createElement("a");

 
 

The TypeScript any hack sidesteps this TypeScript error:

Cannot assign to 'style' because it is a read-only property.

 
 

TypeScript seems to consider the a of HTMLAnchorElement as a type and, hence, there seems to be no way to set a style. Lame! I stole the code you see here from this and it seems gold (in Chrome, but not IE11).

No comments:

Post a Comment