কিভাবে একটি ফাইল সংরক্ষণ করতে হয়

ফাইলগুলির সাথে ডিল করা ওয়েবে অ্যাপগুলির জন্য সবচেয়ে সাধারণ ক্রিয়াকলাপগুলির মধ্যে একটি৷ ঐতিহ্যগতভাবে, ব্যবহারকারীদের একটি ফাইল আপলোড করতে হবে, এতে কিছু পরিবর্তন করতে হবে এবং তারপরে এটি আবার ডাউনলোড করতে হবে, যার ফলে ডাউনলোড ফোল্ডারে একটি অনুলিপি থাকবে। ফাইল সিস্টেম অ্যাক্সেস API-এর মাধ্যমে, ব্যবহারকারীরা এখন সরাসরি ফাইল খুলতে, পরিবর্তন করতে এবং মূল ফাইলে পরিবর্তনগুলি সংরক্ষণ করতে পারে।

আধুনিক উপায়

ফাইল সিস্টেম অ্যাক্সেস API এর showSaveFilePicker() পদ্ধতি ব্যবহার করে

একটি ফাইল সংরক্ষণ করতে, showSaveFilePicker() কল করুন, যা FileSystemFileHandle এর সাথে একটি প্রতিশ্রুতি প্রদান করে। আপনি এই পদ্ধতিতে পছন্দসই ফাইলের নাম পাঠাতে পারেন { suggestedName: 'example.txt' }

ব্রাউজার সমর্থন

  • 86
  • 86
  • এক্স
  • এক্স

উৎস

ক্লাসিক উপায়

<a download> উপাদান ব্যবহার করে

একটি পৃষ্ঠার <a download> উপাদান ব্যবহারকারীকে এটিতে ক্লিক করতে এবং একটি ফাইল ডাউনলোড করতে দেয়। কৌশলটি এখন জাভাস্ক্রিপ্ট সহ একটি পৃষ্ঠায় উপাদানটিকে অদৃশ্যভাবে সন্নিবেশ করা এবং এটিকে প্রোগ্রাম্যাটিকভাবে ক্লিক করা।

ব্রাউজার সমর্থন

  • 15
  • 13
  • 20
  • 10.1

উৎস

প্রগতিশীল বর্ধিতকরণ

নীচের পদ্ধতিটি ফাইল সিস্টেম অ্যাক্সেস API ব্যবহার করে যখন এটি সমর্থিত হয় এবং অন্যথায় ক্লাসিক পদ্ধতিতে ফিরে আসে। উভয় ক্ষেত্রেই ফাংশন ফাইলটি সংরক্ষণ করে, কিন্তু যেখানে ফাইল সিস্টেম অ্যাক্সেস API সমর্থিত হয়, ব্যবহারকারী একটি ফাইল সংরক্ষণ ডায়ালগ পাবেন যেখানে তারা ফাইলটি কোথায় সংরক্ষণ করা হবে তা চয়ন করতে পারে।

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 `<a download>` element and append it invisibly.
  const a = document.createElement('a');
  a.href = blobURL;
  a.download = suggestedName;
  a.style.display = 'none';
  document.body.append(a);
  // Programmatically click the element.
  a.click();
  // Revoke the blob URL and remove the element.
  setTimeout(() => {
    URL.revokeObjectURL(blobURL);
    a.remove();
  }, 1000);
};

আরও পড়া

ডেমো

এইচটিএমএল

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="icon"
      href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎉</text></svg>"
    />
    <title>How to save a file</title>
  </head>
  <body>
    <h1>How to save a file</h1>

    <label
      >Text to save
      <textarea rows="3">
Some sample text for you to save. Feel free to edit this.</textarea
      >
    </label>
    <label>File name <input class="text" value="example.txt" /></label>
    <button class="text" type="button">Save text</button>

    <label
      >Image to save
      <img
        src="https://cdn.glitch.global/75170424-3d76-41d7-ae77-72d0efb0401b/AVIF%20Test%20picture%20(JPEG%20converted%20to%20AVIF%20with%20Convertio).avif?v=1658240752363"
        alt="Blue flower."
        width="630"
        height="420"
    /></label>
    <label>File name <input class="img" value="example.avif" /></label>
    <button class="img" type="button">Save image</button>
  </body>
</html>

সিএসএস


        :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;
}
        

জেএস


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

অন্য কিছু উল্লেখ না করা থাকলে, এই পৃষ্ঠার কন্টেন্ট Creative Commons Attribution 4.0 License-এর অধীনে এবং কোডের নমুনাগুলি Apache 2.0 License-এর অধীনে লাইসেন্স প্রাপ্ত। আরও জানতে, Google Developers সাইট নীতি দেখুন। Java হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।

2024-03-13 UTC-তে শেষবার আপডেট করা হয়েছে।