/
TimurIsm
/
OpenEyes
Обзор
Документация
Войти
/
TimurIsm
/
OpenEyes
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
front/src/components/FileUploader.jsx
66 строк
3 KB
IsmTimur
Initial commit: added backend (Express) and frontend (React Vite)
19 июн 2025, 18:32
19 июн 2025, 18:32
6cf6155
Код
Авторство
О чём код?
import React from 'react'; import mammoth from 'mammoth'; const FileUploader = ({ onFileProcessed }) => { const handleFileUpload = (e) => { const file = e.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (event) => { const arrayBuffer = event.target.result; const processText = (text) => { const normalizedText = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); const paragraphs = normalizedText.split('\n').filter(para => para.trim() !== ''); if (paragraphs.length > 0) { const title = paragraphs[0]; const description = paragraphs.length > 1 ? paragraphs[1] : ''; const content = paragraphs.length > 2 ? paragraphs.slice(2).join('\n\n') : ''; onFileProcessed({ title, description, content }); } }; if (file.name.endsWith('.docx')) { mammoth.extractRawText({ arrayBuffer }) .then((result) => processText(result.value)) .catch((error) => { console.error('Ошибка при чтении файла .docx:', error); alert('Ошибка при чтении файла .docx'); }); } else if (file.name.endsWith('.txt')) { processText(event.target.result); } else { alert('Неподдерживаемый формат файла. Пожалуйста, загрузите файл .txt или .docx.'); } }; if (file.name.endsWith('.docx')) { reader.readAsArrayBuffer(file); } else { reader.readAsText(file, 'UTF-8'); } }; return ( <div className="mb-6 w-1/2 sm:w-1/3 ml-auto"> <label htmlFor="file-upload" className="block mb-2 text-md font-medium text-gray-900"> Импортировать текст из файла .txt или .docx: </label> <input className="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 focus:outline-none" id="file-upload" type="file" accept=".txt, .docx" onChange={handleFileUpload} /> <div className="text-xs text-gray-500 mt-1"> Первый абзац - название. Второй - описание. Третий и далее - содержание. </div> </div> ); }; export default FileUploader;