पाठ 22 / 30
File System API
File System Access API से local files खोलें, बदलें और सेव करें, और सीमित browser support के लिए feature detection करें।
User Consent के साथ File Access
File System Access API आपके app को explicit permission के साथ user के device की files खोलने, पढ़ने और लिखने देता है। Support Chromium browsers तक सीमित है, इसलिए file input और download link जैसा fallback दें।
Files को open और read करें
User files को access करने के लिए showOpenFilePicker का उपयोग करें।
const openFileBtn = document.getElementById('open-file');
openFileBtn.addEventListener('click', async () => {
try {
const [fileHandle] = await window.showOpenFilePicker({
types: [{ description: 'Text files', accept: { 'text/plain': ['.txt'] } }]
});
const file = await fileHandle.getFile();
const content = await file.text();
console.log('File content:', content);
} catch (err) {
console.error('File picker error:', err);
}
});
Output:
User can select files; app reads and processes them directly.