Created
July 13, 2022 10:55
-
-
Save kyungjoongo/0217f74462a0bf26d1e593fefac85e7f 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'; | |
const createFormData = (photo, body = {}) => { | |
console.log("datadatadatadata===>", data); | |
/*Object.keys(body).forEach((key) => { | |
data.append(key, body[key]); | |
});*/ | |
return data; | |
}; | |
const SignPadScreen = () => { | |
const [photo, setPhoto] = React.useState(null); | |
const handleChoosePhoto = () => { | |
const loadImageOption = { | |
selectionLimit: 0, | |
mediaType: 'photo', | |
includeBase64: false, | |
includeExtra: true, | |
} | |
launchImageLibrary(loadImageOption, (response) => { | |
console.log(response); | |
setPhoto(response); | |
}) | |
}; | |
const handleUploadPhoto = () => { | |
const formData = new FormData(); | |
console.log("sldkflskdflksdf===>", photo); | |
let imageList = photo.assets | |
/* formData.append(`photo`, { | |
name: imageList[0].fileName, | |
type: imageList[0].type, | |
uri: Platform.OS === 'ios' ? imageList[0].uri.replace('file://', '') : imageList[0].uri, | |
}); | |
formData.append(`photo`, { | |
name: imageList[1].fileName, | |
type: imageList[1].type, | |
uri: Platform.OS === 'ios' ? imageList[1].uri.replace('file://', '') : imageList[1].uri, | |
});*/ | |
for (let index = 0; index < imageList.length; index++) { | |
formData.append(`photo`, { | |
name: imageList[index].fileName, | |
type: imageList[index].type, | |
uri: Platform.OS === 'ios' ? imageList[index].uri.replace('file://', '') : imageList[index].uri, | |
}); | |
} | |
console.log("formData===>", formData); | |
fetch(`${SERVER_URL}/api/upload`, { | |
method: 'POST', | |
body: formData, | |
}).then((response) => response.json()).then((response) => { | |
console.log('response', response); | |
}).catch((error) => { | |
console.log('error', error); | |
}); | |
}; | |
return ( | |
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}> | |
{photo && ( | |
<> | |
<Image | |
source={{uri: photo.uri}} | |
style={{width: 300, height: 300}} | |
/> | |
<Button title="Upload Photo" onPress={handleUploadPhoto}/> | |
</> | |
)} | |
<Button title="Choose Photo" onPress={handleChoosePhoto}/> | |
</View> | |
); | |
}; | |
export default SignPadScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment