A handy function to prompt users to download file generated with browser JS code in RAM

The following function is quite handy in prompting the user to download content that is generated by browser JS code in memory:

function promptUserToDownload(
  content: BlobPart,
  filename: string,
  mimeType?: string,
): void {
  const anchorElement = document.createElement("a");
  const blob = new Blob([content], { type: mimeType });
  const url = URL.createObjectURL(blob);
  anchorElement.setAttribute("href", url);
  anchorElement.setAttribute("download", filename);
  anchorElement.click();
}

To use it, pass in the content, file name, and optionally the mime type:

promptUserToDownload("a,b,c", "abc.csv", "text/csv");

The user will be prompted to download a file named abc.csv with the content a,b,c.

The promptUserToDownload function is based on an improved version of the solution at javascript - How to create a file in memory for user to download, but not through server? - Stack Overflow