|
@ -1,6 +1,7 @@ |
|
|
import React, { useEffect } from 'react'; |
|
|
import React, { useEffect } from 'react'; |
|
|
import { View, Text, Button } from 'react-native'; |
|
|
import { View, Text, Button } from 'react-native'; |
|
|
import RNFS from 'react-native-fs'; |
|
|
import RNFS from 'react-native-fs'; |
|
|
|
|
|
import { Platform } from 'react-native'; |
|
|
|
|
|
|
|
|
const FileReadWriteExample = () => { |
|
|
const FileReadWriteExample = () => { |
|
|
// 定义文件路径
|
|
|
// 定义文件路径
|
|
@ -35,15 +36,50 @@ const FileReadWriteExample = () => { |
|
|
.then(() => { |
|
|
.then(() => { |
|
|
console.log('Directory created successfully'); |
|
|
console.log('Directory created successfully'); |
|
|
}) |
|
|
}) |
|
|
.catch((error) => { |
|
|
|
|
|
|
|
|
.catch(error => { |
|
|
console.error('Error creating directory:', 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 ( |
|
|
return ( |
|
|
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
|
|
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
|
|
<Button title="Write to File" onPress={writeToFile} /> |
|
|
<Button title="Write to File" onPress={writeToFile} /> |
|
|
<Button title="Read from File" onPress={readFromFile} /> |
|
|
<Button title="Read from File" onPress={readFromFile} /> |
|
|
|
|
|
<Button title="测试上传" onPress={uploadFile} /> |
|
|
</View> |
|
|
</View> |
|
|
); |
|
|
); |
|
|
}; |
|
|
}; |
|
|