diff --git a/components/ui/FileReadWriteExample.tsx b/components/ui/FileReadWriteExample.tsx index 1f571d2..59b9020 100644 --- a/components/ui/FileReadWriteExample.tsx +++ b/components/ui/FileReadWriteExample.tsx @@ -1,6 +1,7 @@ import React, { useEffect } from 'react'; import { View, Text, Button } from 'react-native'; import RNFS from 'react-native-fs'; +import { Platform } from 'react-native'; const FileReadWriteExample = () => { // 定义文件路径 @@ -35,17 +36,52 @@ const FileReadWriteExample = () => { .then(() => { console.log('Directory created successfully'); }) - .catch((error) => { + .catch(error => { console.error('Error creating directory:', error); }); }, []); + const uploadFile = async () => { + const formData = new FormData(); + const fileName = filePath.split('/').pop(); + const fileContent = await RNFS.readFile(filePath, 'utf8'); + + const fileToAppend = { + // uri: Platform.OS === 'android' ? filePath : filePath.replace('file://', ''), + uri: `file://${filePath}`, + type: 'text/plain', + name: fileName, + // blobValue: new Blob([fileContent], { type: 'text/plain' }) + }; + formData.append('file', fileToAppend as any); + + const response = await fetch('http://192.168.1.114:8080/upload', { + method: 'POST', + // headers: { + // // 注意:不要手动设置 Content-Type,让浏览器自动设置 + // 'Content-Type': 'multipart/form-data' + // }, + body: formData, + }); + // const response = await fetch('http://192.168.1.114:8080/api/standard-rail/list', { + // method: 'POST', + // }); + + if (response.ok) { + // const json = await response.json(); + // console.log(json); + console.log('文件上传成功'); + } else { + console.log('上传失败:' + response.status); + } + }; return (