Created
July 15, 2022 11:50
-
-
Save kyungjoongo/e51ecce821f3dfe085e94b9da0fc93d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState} from 'react'; | |
import {Button, Image, Platform, View} from 'react-native'; | |
import {launchImageLibrary} from 'react-native-image-picker'; | |
const SERVER_URL = 'http://kyungjoon2.ipdisk.co.kr:3000/upload'; | |
const UploadDemo = () => { | |
const [reg_file, set_reg_files] = useState(null); | |
const [ext_file, set_ext_files] = useState(null); | |
const handleChoosePhoto = () => { | |
const loadImageOption = { | |
selectionLimit: 0, | |
mediaType: 'photo', | |
includeBase64: false, | |
includeExtra: true, | |
} | |
launchImageLibrary(loadImageOption, (response) => { | |
console.log(response); | |
set_reg_files(response); | |
}) | |
}; | |
const handleChoosePhoto2 = () => { | |
const loadImageOption = { | |
selectionLimit: 0, | |
mediaType: 'photo', | |
includeBase64: false, | |
includeExtra: true, | |
} | |
launchImageLibrary(loadImageOption, (response) => { | |
console.log(response); | |
set_ext_files(response); | |
}) | |
}; | |
const handleUploadPhoto = () => { | |
const formData = new FormData(); | |
alert('upload') | |
console.log("sldkflskdflksdf===>", reg_file); | |
let reg_files = reg_file.assets | |
const data = { | |
"name": "kyungjoongo", | |
"nickname" : 'dsflskdflksdf_kyungjoon' | |
} | |
for (let key in data) { | |
formData.append(key, data[key]); | |
} | |
for (let index = 0; index < reg_files.length; index++) { | |
formData.append(`reg_file`, { | |
name: reg_files[index].fileName, | |
type: reg_files[index].type, | |
uri: Platform.OS === 'ios' ? reg_files[index].uri.replace('file://', '') : reg_files[index].uri, | |
}); | |
} | |
console.log("formData===>" ,formData); | |
fetch(SERVER_URL, { | |
method: 'POST', | |
body: formData, | |
}).then((response) => response.json()).then((response) => { | |
alert('response', response); | |
}).catch((error) => { | |
alert('error', error); | |
}); | |
}; | |
return ( | |
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}> | |
{reg_file && ( | |
<> | |
<Image | |
source={{uri: reg_file.uri}} | |
style={{width: 300, height: 300}} | |
/> | |
<Button title="Upload Photo" onPress={handleUploadPhoto}/> | |
</> | |
)} | |
<Button title="Choose Photo-reg_file" onPress={handleChoosePhoto}/> | |
{/*<Button title="Choose Photo-ext_file" onPress={handleChoosePhoto2}/>*/} | |
</View> | |
); | |
}; | |
export default UploadDemo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment