शेयर की गई फ़ाइलें कैसे पाएं

Palances Liao
Palances Liao

मॉडर्न तरीका

Web Share Target API का इस्तेमाल करना

सबसे पहले, अपने वेब ऐप्लिकेशन के मेनिफ़ेस्ट में share_target के बारे में बताएं. इसमें action (शेयर की गई फ़ाइलों को मैनेज करने वाला आपका यूआरएल), method (फ़ाइलों के लिए "POST"), enctype (फ़ाइलों के लिए "multipart/form-data") और params ऑब्जेक्ट शामिल होना चाहिए. इस ऑब्जेक्ट में files प्रॉपर्टी होती है. इसमें ऑब्जेक्ट की एक ऐसी कैटगरी होती है जिसमें name और accept प्रॉपर्टी होती है. इनमें शेयर की जा सकने वाली फ़ाइल टाइप और उन्हें पाने के लिए नाम की सूची होती है.

{
  "share_target": {
    "action": "/receive-files/",
    "method": "POST",
    "enctype": "multipart/form-data",
    "params": {
      "files": [
        {
          "name": "image",
          "accept": ["image/jpeg", "image/png", "image/webp", "image/gif"]
        }
      ]
    }
  }
}

इसके बाद, आपको सर्विस वर्कर में आने वाले POST अनुरोधों को मैनेज करना होगा. फ़ाइल को कुछ समय के लिए मीडिया कैश में सेव किया जाता है, ताकि इसे क्लाइंट में इस्तेमाल किया जा सके. ऐसा, ऐप्लिकेशन को 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);
      })(),
    );
  }
});

आखिर में, आपको क्लाइंट में फ़ाइल का इस्तेमाल करना होगा.

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.
    }
  }
});

इस बारे में और पढ़ें

डेमो

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="manifest" href="manifest.json" />
    <title>How to receive shared files</title>
    <link rel="stylesheet" href="style.css" />
    <!-- TODO: Devsite - Removed inline handlers -->
    <!-- <script>
      if ('serviceWorker' in navigator) {
        window.addEventListener('load', async () => {
          const registration = await navigator.serviceWorker.register(
            'sw.js',
          );
          console.log(
            'Service worker registered for scope',
            registration.scope,
          );
        });
      }
    </script>
    <script src="script.js" type="module"></script> -->
  </head>
  <body>
    <h1>How to receive shared files</h1>
    <p>Install the app. After the installation, try sharing an image to it from another app.
  </body>
</html>

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.
    }
  }
});