--- url: "https://web.dev/articles/files/receive-shared-files" titulek: "How to receive shared files | Files and directories patterns | web.dev" h1: "How to receive shared files Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/files/receive-shared-files" drobecky: ["Files and directories patterns", "How to receive shared files"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:04:41" znaku: 4035 slov: 539 kodovani: "iso-8859-2" sha256: "bba9ad7ca74593a7e83d8f99686cb235bcecc4d3bfd36a7255bf111c30becd98" --- How to receive shared files Stay organized with collections Save and categorize content based on your preferences. Palances Liao The modern way First, declare a `share_target` in your web app manifest that lists an `action` (your URL that handles shared files), a `method` (`"POST"` for files) and an `enctype` (`"multipart/form-data"` for files) and a `params` object that contains a `files` property with an array of objects with a `name` and `accept` property that list the sharable file types and the name to obtain them. `{ "share_target": { "action": "/receive-files/", "method": "POST", "enctype": "multipart/form-data", "params": { "files": [ { "name": "image", "accept": ["image/jpeg", "image/png", "image/webp", "image/gif"] } ] } } }` You then need to handle incoming `POST` requests in the service worker. The file is temporarily stored in a media cache, so it can be consumed in the client. This can be done by redirecting the app to a URL with a special marker query parameter like `share-target`. `self.addEventListener('fetch', (fetchEvent) => { if (fetchEvent.request.url.endsWith('/receive-files/') && fetchEvent.request.method === 'POST') { return fetchEvent.respondWith( (async () => { const formData = await fetchEvent.request.formData(); const image = formData.get('image'); const keys = await caches.keys(); const mediaCache = await caches.open(keys.filter((key) => key.startsWith('media'))[0]); await mediaCache.put('shared-image', new Response(image)); return Response.redirect('./?share-target', 303); })(), ); } });` Finally, you need to consume the file in the client. `window.addEventListener('load', async () => { if (location.search.includes('share-target')) { const keys = await caches.keys(); const mediaCache = await caches.open( keys.filter((key) => key.startsWith('media'))[0], ); const image = await mediaCache.match('shared-image'); if (image) { const blob = await image.blob(); await mediaCache.delete('shared-image'); // Handle the shared file somehow. } } });` Further reading [Receiving shared data with the Web Share Target API](/web-share-target) [W3C Web Share Target API](https://w3c.github.io/web-share-target/) Demo HTML ` How to receive shared files

How to receive shared files

Install the app. After the installation, try sharing an image to it from another app. ` CSS `html { box-sizing: border-box; font-family: system-ui, sans-serif; color-scheme: dark light; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; } img { height: auto; max-width: 100%; display: block; }` JS `window.addEventListener('load', async () => { if (location.search.includes('share-target')) { const keys = await caches.keys(); const mediaCache = await caches.open( keys.filter((key) => key.startsWith('media'))[0], ); const image = await mediaCache.match('shared-image'); if (image) { const blob = await image.blob(); await mediaCache.delete('shared-image'); // Handle the shared file somehow. } } });` 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.