--- url: "https://web.dev/articles/files/save-a-file" titulek: "How to save a file | Files and directories patterns | web.dev" h1: "How to save a file Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/files/save-a-file" drobecky: ["Files and directories patterns", "How to save a file"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:04:15" znaku: 6742 slov: 897 kodovani: "iso-8859-2" sha256: "6e08a6735ed3c0be8332b09227878e79cfe828cc4a7e9f7ae0b63d81fea1277c" --- How to save a file Stay organized with collections Save and categorize content based on your preferences. Thomas Steiner Dealing with files is one of the most common operations for apps on the web. Traditionally, users needed to upload a file, make some changes to it, and then download it again, resulting in a copy in the Downloads folder. With the File System Access API, users can now open files directly, make modifications, and save back the changes to the original file. The modern way Using the File System Access API's `showSaveFilePicker()` method To save a file, call [`showSaveFilePicker()`](https://developer.mozilla.org/docs/Web/API/Window/showSaveFilePicker), which returns a promise with [`FileSystemFileHandle`](https://developer.mozilla.org/docs/Web/API/FileSystemFileHandle). You can pass the desired file name to the method as `{ suggestedName: 'example.txt' }`. Browser Support 86 86 x x [Source](https://developer.mozilla.org/docs/Web/API/Window/showSaveFilePicker) The classic way Using the `` element The `` element on a page allows the user to click it and download a file. The trick now consists of inserting the element invisibly into a page with JavaScript and clicking it programmatically. Browser Support 15 13 20 10.1 [Source](https://developer.mozilla.org/docs/Web/API/HTMLAnchorElement/download) Progressive enhancement The method below uses the File System Access API when it's supported and else falls back to the classic approach. In both cases the function saves the file, but in case of where the File System Access API is supported, the user will get a file save dialog where they can choose where the file should be saved. `const saveFile = async (blob, suggestedName) => { // Feature detection. The API needs to be supported // and the app not run in an iframe. const supportsFileSystemAccess = 'showSaveFilePicker' in window && (() => { try { return window.self === window.top; } catch { return false; } })(); // If the File System Access API is supported… if (supportsFileSystemAccess) { try { // Show the file save dialog. const handle = await showSaveFilePicker({ suggestedName, }); // Write the blob to the file. const writable = await handle.createWritable(); await writable.write(blob); await writable.close(); return; } catch (err) { // Fail silently if the user has simply canceled the dialog. if (err.name !== 'AbortError') { console.error(err.name, err.message); } return; } } // Fallback if the File System Access API is not supported… // Create the blob URL. const blobURL = URL.createObjectURL(blob); // Create the `` element and append it invisibly. const a = document.createElement('a'); a.href = blobURL; a.download = suggestedName; a.style.display = 'none'; document.body.append(a); // Programmatically click the element. a.click(); // Revoke the blob URL and remove the element. setTimeout(() => { URL.revokeObjectURL(blobURL); a.remove(); }, 1000); };` Further reading [File System Access API](/file-system-access) Demo HTML ` How to save a file

How to save a file

` CSS `:root { color-scheme: dark light; } html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; font-family: system-ui, sans-serif; } img { max-width: 320px; height: auto; } label, button, textarea, input, img { display: block; margin-block: 1rem; }` JS `const textarea = document.querySelector('textarea'); const textInput = document.querySelector('input.text'); const textButton = document.querySelector('button.text'); const img = document.querySelector('img'); const imgInput = document.querySelector('input.img'); const imgButton = document.querySelector('button.img'); const saveFile = async (blob, suggestedName) => { // Feature detection. The API needs to be supported // and the app not run in an iframe. const supportsFileSystemAccess = 'showSaveFilePicker' in window && (() => { try { return window.self === window.top; } catch { return false; } })(); // If the File System Access API is supported… if (supportsFileSystemAccess) { try { // Show the file save dialog. const handle = await showSaveFilePicker({ suggestedName, }); // Write the blob to the file. const writable = await handle.createWritable(); await writable.write(blob); await writable.close(); return; } catch (err) { // Fail silently if the user has simply canceled the dialog. if (err.name !== 'AbortError') { console.error(err.name, err.message); return; } } } // Fallback if the File System Access API is not supported… // Create the blob URL. const blobURL = URL.createObjectURL(blob); // Create the `` element and append it invisibly. const a = document.createElement('a'); a.href = blobURL; a.download = suggestedName; a.style.display = 'none'; document.body.append(a); // Click the element. a.click(); // Revoke the blob URL and remove the element. setTimeout(() => { URL.revokeObjectURL(blobURL); a.remove(); }, 1000); }; textButton.addEventListener('click', async () => { const blob = new Blob([textarea.value], { type: 'text/plain' }); await saveFile(blob, textInput.value); }); imgButton.addEventListener('click', async () => { const blob = await fetch(img.src).then((response) => response.blob()); await saveFile(blob, imgInput.value); });` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-09-04 UTC.