Skip to content

Instantly share code, notes, and snippets.

@kyungjoongo
Created July 13, 2022 10:55
Show Gist options
  • Save kyungjoongo/5cea335958af388a07e8ff2a526cb0df to your computer and use it in GitHub Desktop.
Save kyungjoongo/5cea335958af388a07e8ff2a526cb0df to your computer and use it in GitHub Desktop.
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