Created
July 13, 2022 19:59
-
-
Save kyungjoongo/c017f84a3b6ab01faa901587470ecea1 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 from 'react'; | |
import {View, Image, Button, Platform} from 'react-native'; | |
import {launchImageLibrary} from 'react-native-image-picker'; | |
const SERVER_URL = 'http://kyungjoon2.ipdisk.co.kr:3000/upload'; | |
const createFormData = (photo, body = {}) => { | |
console.log("datadatadatadata===>", data); | |
/*Object.keys(body).forEach((key) => { | |
data.append(key, body[key]); | |
});*/ | |
return data; | |
}; | |
const UploadDemo = () => { | |
const [reg_file, set_reg_files] = React.useState(null); | |
const [ext_file, set_ext_files] = React.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 | |
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, | |
}); | |
} | |
let ext_files = ext_file.assets | |
for (let index = 0; index < ext_files.length; index++) { | |
formData.append(`reg_file`, { | |
name: ext_files[index].fileName, | |
type: ext_files[index].type, | |
uri: Platform.OS === 'ios' ? ext_files[index].uri.replace('file://', '') : ext_files[index].uri, | |
}); | |
} | |
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="Upload Photo" onPress={handleUploadPhoto2}/> | |
</> | |
)} | |
<Button title="Choose Photo" onPress={handleChoosePhoto}/> | |
</View> | |
); | |
}; | |
export default UploadDemo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment