Created
July 13, 2022 20:12
-
-
Save kyungjoongo/2fa21b73e5904e1ed9765fab2b96393f 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(`ext_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, | |
}); | |
} | |
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