| 1 |
/** |
| 2 |
* Download a file from the provided string. |
| 3 |
* |
| 4 |
* @param content |
| 5 |
* @param fileNamePrefix |
| 6 |
*/ |
| 7 |
export const downloadFileFromString = (content, fileNamePrefix) => { |
| 8 |
|
| 9 |
const blob = content; |
| 10 |
|
| 11 |
// Create a temporary URL to the blob. |
| 12 |
const url = window.URL.createObjectURL(new Blob([blob])); |
| 13 |
|
| 14 |
// Create a link element. |
| 15 |
const link = document.createElement('a'); |
| 16 |
link.href = url; |
| 17 |
const fileName = fileNamePrefix + '-' + Date.now().toString().slice(0, 10) + '.csv'; |
| 18 |
link.setAttribute('download', fileName); // Specify the filename |
| 19 |
|
| 20 |
// Append the link to the body. |
| 21 |
document.body.appendChild(link); |
| 22 |
|
| 23 |
// Trigger the click event on the link. |
| 24 |
link.click(); |
| 25 |
|
| 26 |
// Cleanup. |
| 27 |
link.parentNode.removeChild(link); |
| 28 |
|
| 29 |
} |