In this video, you gave Sam’s recipe box an Export a backup button. It saves the whole box as a file in Sam’s Downloads folder, so Clear my data, a cleared browser or a new laptop can’t lose the recipes.
Key Points
- A page can’t write to the disk itself. In the last lesson, the page could only read a file Sam picked. Saving works the same way. The page hands the browser a new file, and the browser saves it.
- Browsers save files from download links, so the listener builds one from the text, a Blob, an address and a link.
- First, the text.
JSON.stringify({ version: 3, recipes }, null, 2)turns Sam’s recipes into JSON, marked version 3 because they now carry steps. The box’s own save inlocalStoragestill says 2. The end of the course moves it to 3, with a load that reads both. In your own box, only the recipes that came with steps have them yet, so export again once the project’s first step saves version 3. Thenulland the2indent the text, so Sam can read the file. - Then a Blob. A Blob is a chunk of data the browser holds in memory, and the address, the next part, needs one. The
Fileyou read in Open a File the User Picks is a Blob with a name.new Blob([text], { type: "application/json" })takes an array of pieces, joined end to end (here there’s one, the whole text), and atypethat says it holds JSON text. - Then an address. A link needs an address, and
URL.createObjectURL(blob)makes one. It starts withblob:, then the start of the page’s address without the path, then a random id, such asblob:http://127.0.0.1:3000/d9785306-a1f4-4f90-bb45-04765f61be23for a page athttp://127.0.0.1:3000/recipes/index.html. It points at the Blob in the browser’s memory, not at a file on the disk. - Then the link.
document.createElement("a")makes a link element, writtenain HTML. It’s never added to the page, so Sam never sees it. Itshrefis the Blob’s address. - Click it from code. The link’s
downloadproperty names the file and tells the browser to save it rather than open it. Without it, the tab opens the JSON instead.link.click()clicks the link from code, even off the page. Browsers can block downloads that don’t start from a user’s click, and this one runs inside Sam’s own click on Export a backup. - Let the address go. The address keeps the Blob in memory until the page closes. So a second after the click, once the download has its copy, a timer calls
URL.revokeObjectURL(url), and the Blob can go. - What you see. Click Export a backup, and Chrome’s download icon shows
recipe-box-backup.jsonwith3.3 KB • Done. The file is in Downloads, besideayumi.csv, and holds"version": 3and Sam’s seven recipes (your count depends on what your box holds). The page doesn’t change. Chrome saved to Downloads without asking, and it can be set to ask Sam instead. - Every export is a new file. After Sam adds a recipe (Shakshuka in the video; use any new name in yours) and exports again, Chrome’s default settings save
recipe-box-backup (1).jsonwith eight recipes. The first backup still holds seven, andayumi.csvis unchanged. The page asked for a download, and the browser named the file. If Chrome asks where to save, Sam decides whether to replace the old one. - The rule: with a file input and a download link, the page never changes a file on the disk. To save, it hands the browser a new file: the text in a Blob, a blob address, and a link that downloads it.
- Reading a saved file back uses your Choose File handler, with
JSON.parse()insidetry...catch. You’ll do both directions for Sam’s shopping list in the next exercise, and Sam’s recipe box gets its own import at the end of the course.
The Code
Open Sam’s recipe box through Live Preview at http://127.0.0.1:3000/recipes/index.html. In recipes/index.html, add the button right after the closing </label> of the Import a recipe list control: