CREATES FILE AND DOWNLOADS

by Emily Humphrey

JavaScript

// Step 1: Create a Blob with "Hello World" content
const content = "Hello World";
const blob = new Blob([content], { type: 'text/plain' });

// Step 2: Create a URL for the Blob
const fileURL = URL.createObjectURL(blob);

// Step 3: Create an anchor element
const downloadLink = document.createElement('a');

// Step 4: Set the href to the Blob URL
downloadLink.href = fileURL;

// Step 5: Suggest a filename for the download
downloadLink.download = 'hello-world.txt';

// Step 6: Simulate a click on the anchor element
document.body.appendChild(downloadLink); // Append to the document
downloadLink.click(); // Trigger the download

// Step 7: Optionally, revoke the Blob URL
URL.revokeObjectURL(fileURL);
downloadLink.remove(); // Clean up