save cmbuffer to file in javascript

To save cmbuffer to a file in JavaScript, you can use the following code assuming that the data is in cmbuffer variable:

index.tsx
// Create blob from the cmbuffer variable
const blob = new Blob([cmbuffer], {type: 'application/octet-stream'});

// Create download link
const url = URL.createObjectURL(blob);

const a = document.createElement('a');
a.href = url;
a.download = 'filename.ext';

// Append link to the body
document.body.appendChild(a);

// Click the link to download the file
a.click();

// Remove the link from the body
document.body.removeChild(a);

// Revoke the blob URL
URL.revokeObjectURL(url);
483 chars
22 lines

This code creates a blob from cmbuffer and generates a download link, which is appended to the body of the page. When the link is clicked, the file is downloaded and then the link is removed from the body. Finally, the blob URL is revoked to free up memory.

related categories

gistlibby LogSnag