Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Save a Backup File

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 in localStorage still 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. The null and the 2 indent 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 File you 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 a type that says it holds JSON text.
  • Then an address. A link needs an address, and URL.createObjectURL(blob) makes one. It starts with blob:, then the start of the page’s address without the path, then a random id, such as blob:http://127.0.0.1:3000/d9785306-a1f4-4f90-bb45-04765f61be23 for a page at http://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, written a in HTML. It’s never added to the page, so Sam never sees it. Its href is the Blob’s address.
  • Click it from code. The link’s download property 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.json with 3.3 KB • Done. The file is in Downloads, beside ayumi.csv, and holds "version": 3 and 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).json with eight recipes. The first backup still holds seven, and ayumi.csv is 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() inside try...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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this video, Sam’s recipe box saves a backup file. Sam’s seven recipes live in localStorage, inside the browser. Clear my data, a cleared browser or a new laptop, and they’re gone.

00:13 A backup file in Sam’s Downloads folder would keep them safe. In the last lesson, the page could only read a file Sam picked. Saving works the same way: the page can’t write a file to the disk itself.

00:27 Instead, it hands the browser a new file, and the browser saves it.

00:33 In index.html, add a button, Export a backup, right after the import control. In main.js, inside setUpRecipeBox, give it a click listener. The full code is below the video, to copy.

00:48 Browsers save files from download links, so the listener builds one in four parts. First, the text: Sam’s recipes, marked version 3, because they now carry steps.

00:59 The box’s own save still says 2. The end of the course moves it to 3, with a load that reads both. JSON.stringify turns it into JSON. The null and the 2 indent it, so Sam can read the file.

01:15 Second, wrap the text in a Blob, a chunk of data the browser holds in memory. The address, part three, needs one. The File you read in the last lesson is a Blob with a name.

01:26 The array holds the Blob’s pieces, joined end to end. Here there’s one, the whole text. The type says it holds JSON text.

01:37 Third, give the Blob an address, because a link needs one. URL.createObjectURL makes it. It starts with blob, then the start of the page’s address, without the path.

01:49 Then a random id. It points at the Blob in the browser’s memory, not at a file on the disk.

01:57 Last, the link. createElement makes a link element, written a in HTML. It’s never added to the page, so Sam never sees it. Its href is the Blob’s address.

02:11 Its download property names the file, and tells the browser to save it rather than open it. Without it, the tab opens the JSON instead. Then calling click clicks it from code, even off the page.

02:24 Browsers can block downloads that don’t start from a user’s click, and this one runs inside Sam’s own click.

02:32 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, and the Blob can go.

02:47 Save main.js, and click Export a backup. Chrome’s download icon shows recipe-box-backup.json, 3.3 kilobytes, Done. The file is in Downloads, beside Ayumi’s list.

03:02 In your editor, it holds version 3 and Sam’s seven recipes. The page didn’t change. Chrome chose Downloads without asking. It can be set to ask Sam instead.

03:15 Now Sam adds Shakshuka and clicks Export a backup again. Downloads already holds a file with that name. Your turn: pause the video and predict. Does the new backup replace the old one?

03:32 No. With Chrome’s default settings, it saves a second file, with a one in brackets in its name and eight recipes. The first backup still holds seven, and ayumi.csv is unchanged.

03:46 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.

03:56 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.

04:10 Reading a saved file back uses your Choose File handler, with JSON.parse inside try…catch. Next, in Exercise: Back Up the Shopping List, you’ll save Sam’s shopping list to a file and read it back.

04:23 Sam’s recipe box gets its own import at the end of the course.

Become a Member to join the conversation.