React 앱에서 사용자 파일에 직접 접근할 수 있다면 기능 확장이 무궁무진합니다. 파일 시스템 접근 API(File System Access API)는 브라우저가 지원하는 최신 API로, 사용자 로컬 파일을 읽고 저장할 수 있게 해줍니다. 이번 글에서는 React와 함께 파일 시스템 접근 API를 활용하는 방법을 실무 중심으로 소개합니다.
1. 파일 시스템 접근 API란?
파일 시스템 접근 API는 웹 앱이 사용자의 로컬 파일과 폴더를 직접 읽고 쓸 수 있도록 하는 최신 웹 표준 API입니다. 기존에 파일 입력 창으로 제한되던 사용자 파일 접근 권한을 확장해 앱의 편의성과 기능성을 크게 향상시킵니다.
2. React에서 파일 열기 구현하기
파일 열기 기능은 window.showOpenFilePicker() 함수를 이용해 구현합니다. 선택된 파일은 File 객체로 변환되어 React 상태로 관리할 수 있습니다.
const [fileContent, setFileContent] = React.useState("");
const openFile = async () => {
try {
const [fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
const text = await file.text();
setFileContent(text);
} catch (error) {
console.error('파일 열기 실패:', error);
}
};
3. React에서 파일 저장 구현하기
사용자가 데이터를 편집한 뒤 로컬에 저장할 수 있도록 window.showSaveFilePicker()를 사용해 저장 기능을 구현합니다.
const saveFile = async (content) => {
try {
const fileHandle = await window.showSaveFilePicker({
types: [{
description: '텍스트 파일',
accept: { 'text/plain': ['.txt'] },
}],
});
const writable = await fileHandle.createWritable();
await writable.write(content);
await writable.close();
alert('파일 저장 완료');
} catch (error) {
console.error('파일 저장 실패:', error);
}
};
4. 상세 예제: 텍스트 파일 에디터 만들기
아래는 React 컴포넌트로 작성한 간단한 텍스트 파일 열기 및 저장 에디터 예제입니다.
import React, { useState } from 'react';
function FileEditor() {
const [content, setContent] = useState("");
const handleOpen = async () => {
try {
const [fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
const text = await file.text();
setContent(text);
} catch (e) {
console.error(e);
}
};
const handleSave = async () => {
try {
const fileHandle = await window.showSaveFilePicker({
types: [{
description: 'Text Files',
accept: { 'text/plain': ['.txt'] },
}],
});
const writable = await fileHandle.createWritable();
await writable.write(content);
await writable.close();
alert('파일이 저장되었습니다.');
} catch (e) {
console.error(e);
}
};
return (
);
}
export default FileEditor;
5. 주의사항과 브라우저 지원
파일 시스템 접근 API는 Chromium 기반 최신 브라우저에서 주로 지원됩니다. Firefox, Safari 등에서는 제한적이거나 지원하지 않으므로, 폴백 처리 또는 사용자 안내가 필요합니다.
또한, 보안과 프라이버시를 위해 사용자 동의가 반드시 필요하며, https 환경에서만 동작합니다.
마무리
React 앱에서 파일 시스템 접근 API를 활용하면 로컬 파일과 직접 상호작용하는 강력한 웹 애플리케이션을 만들 수 있습니다. 최신 기술 동향에 맞게 점진적으로 적용해 보시기 바랍니다.
'React' 카테고리의 다른 글
| React 앱에서 AR(증강현실) 콘텐츠 뷰어 구현하기 (0) | 2026.07.14 |
|---|---|
| React에서 이메일 템플릿 미리보기 UI 개발하기 (0) | 2026.07.13 |
| React에서 컴포넌트 렌더링 조건 최적화하기 (0) | 2026.07.12 |
| React 앱에서 스마트폰 센서 데이터 통합 처리 (0) | 2026.07.12 |
| React에서 애플리케이션 Configuration 파일 동적 로드하기 (0) | 2026.07.10 |