--- url: "https://web.dev/articles/files/share-files" titulek: "How to share files | Files and directories patterns | web.dev" h1: "How to share files Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/files/share-files" drobecky: ["Files and directories patterns", "How to share files"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:49:00" znaku: 5228 slov: 663 kodovani: "iso-8859-2" sha256: "5fa02fc5841d7c59b61335e19c466560c8c4f80c1079adbb5aac38fb27041ca9" --- How to share files Stay organized with collections Save and categorize content based on your preferences. Palances Liao The modern way To share files, call [`navigator.share()`](https://developer.mozilla.org/docs/Web/API/Navigator/share). Note that you should always check if files can be shared with [`navigator.canShare()`](https://developer.mozilla.org/docs/Web/API/Navigator/canShare) and make sure your site is using HTTPS before calling the `share()` method. `// Assume `blob` is a PNG image file. const data = { files: [ new File([blob], 'image.png', { type: blob.type, }), ], title: 'My title', text: 'My text', }; if (navigator.canShare(data)) { await navigator.share(data); }` Browser Support 128 93 12.1 [Source](https://developer.mozilla.org/docs/Web/API/Navigator/share) The classic way The next best thing to offer to the user if the Web Share API is not supported is falling back to letting the user download the file, so they can share it manually, for example, via email or via a messenger or online social network app. Browser Support 15 13 20 10.1 [Source](https://developer.mozilla.org/docs/Web/API/HTMLAnchorElement/download) Progressive enhancement The method below uses the Web Share API when the browser supports it and when the data can be shared based on the [supported file types](https://developer.mozilla.org/docs/Web/API/Navigator/share#shareable_file_types). Else, it falls back to downloading the file. `const button = document.querySelector('button'); const img = document.querySelector('img'); // Feature detection const webShareSupported = 'canShare' in navigator; // Update the button action text. button.textContent = webShareSupported ? 'Share' : 'Download'; const shareOrDownload = async (blob, fileName, title, text) => { // Using the Web Share API. 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 implementation. 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…'); });` Further reading [Web Share API on MDN](https://developer.mozilla.org/docs/Web/API/Navigator/share) [Integrate with the OS sharing UI with the Web Share API](/web-share) Demo 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-09-04 UTC.