--- url: "https://web.dev/articles/files/save-a-file/pattern" titulek: "web.dev" h1: "" popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/files/save-a-file/pattern" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:04:57" znaku: 3761 slov: 472 kodovani: "iso-8859-2" sha256: "e999936c9577f0c7d0e15666e1ab9574a888279639319cacb537d49339080429" --- Stay organized with collections Save and categorize content based on your preferences. 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-07-29 UTC.