-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp3.js
More file actions
74 lines (72 loc) · 3.03 KB
/
App3.js
File metadata and controls
74 lines (72 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { Component } from 'react'
import { Text, StyleSheet, View, Button, FlatList, TextInput, Image } from 'react-native'
import fs from 'react-native-fs'
export default class App3 extends Component {
state={files:[],fileName:'20181205063',source:{}}
_createFile=()=>{
//packagename/files/:fs.DocumentDirectoryPath
// /sdcard/Download/: fs.DownloadDirectoryPath
let path=fs.DocumentDirectoryPath+"/test1.txt"
fs.writeFile(path,"一次学习,处处采坑!","utf8")
.then(()=>console.log("文件创建成功:"+path))
.catch(err=>console.log(err))
//EventLoop
}
_readFile=()=>{
let path=fs.DocumentDirectoryPath+"/test1.txt"
fs.readFile(path,'utf8')
.then(content=>console.log(content))
}
_deleteFlie=()=>{
let path=fs.DocumentDirectoryPath+"/test1.txt"
fs.unlink(path)
.then(()=>console.log("删除文件成功"))
}
_readDir=()=>{
let path=fs.DocumentDirectoryPath
fs.readDir(path)
.then(files=>{
let temp=files.map(file=>file.path.split('/').slice(-1)[0])
this.setState({files:temp})
})
}
_downloadFile=()=>{
let path=fs.DocumentDirectoryPath
fs.downloadFile({
fromUrl:'http://johnyu.cn/s181/'+this.state.fileName+'.jpg',toFile:path+'/'+this.state.fileName
}).promise.then(result=>{
let source={uri:'file://'+path+'/'+this.state.fileName}
this.setState({source})
})
}
_upLoadFile=()=>{
let path="file://"+fs.DocumentDirectoryPath+"/20181205072.jpg"
let formData=new FormData()
let options={type:"multipart/form-data"/*封装类型 */,name:"20181205072.jpg",uri:path}
formData.append("file",options)//file是接受的域名
fetch("http://johnyu.cn:7006/upload"/*服务器上传地址 */,{method:"POST",header:{"Content-Type":"multipart/form-data"},body:formData})
.then(resp=>resp.text())
.then(rs=>console.log(rs))
}
_fileNameChange=fileName=>this.setState({fileName})
_renderFile=({item:file})=><View>
<Text>{file}</Text>
</View>
render() {
let index=0
return (
<View>
<Button title="创建文件" onPress={this._createFile}/>
<Button title="读文件内容" onPress={this._readFile}/>
<Button title="删除文件" onPress={this._deleteFlie}/>
<Button title="读出所有文件" onPress={this._readDir}/>
<TextInput value={this.state.fileName} onChangeText={this._fileNameChange} keyboardType="numeric"/>
<Button title="下载文件" onPress={this._downloadFile}/>
<Button title="上传文件" onPress={this._upLoadFile}/>
<FlatList data={this.state.files} renderItem={this._renderFile} keyExtractor={()=>++index}/>
<Image source={this.state.source} style={{height:300,width:200}}/>
</View>
)
}
}
const styles = StyleSheet.create({})