--- url: "https://web.dev/articles/files/share-files/pattern" titulek: "web.dev" h1: "" popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/files/share-files/pattern" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:35:23" znaku: 2514 slov: 319 kodovani: "iso-8859-2" sha256: "380d6c5db9108607bb7c5758c146259227bd6941758db3882e232c75b7a48711" --- Stay organized with collections Save and categorize content based on your preferences. HTML `

Share a file

A cat walking in the snow. ` 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, video { height: auto; max-width: 100%; display: block; } button { margin: 1rem; }` JS `const button = document.querySelector('button'); const img = document.querySelector('img'); const webShareSupported = 'canShare' in navigator; button.textContent = webShareSupported ? 'Share' : 'Download'; const shareOrDownload = async (blob, fileName, title, text) => { if (webShareSupported) { const data = { files: [ new File([blob], fileName, { type: blob.type, }), ], title, text, }; if (navigator.canShare(data)) { try { await navigator.share(data); } catch (err) { if (err.name !== 'AbortError') { console.error(err.name, err.message); } } finally { return; } } } // Fallback const a = document.createElement('a'); a.download = fileName; a.style.display = 'none'; a.href = URL.createObjectURL(blob); a.addEventListener('click', () => { setTimeout(() => { URL.revokeObjectURL(a.href); a.remove(); }, 1000) }); document.body.append(a); a.click(); }; button.addEventListener('click', async () => { const blob = await fetch(img.src).then(res => res.blob()); await shareOrDownload(blob, 'cat.png', 'Cat in the snow', 'Getting cold feet…'); });` 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.