问题
takePicture = async function() {
if (this.camera) {
const options = { quality: 0.5, base64: true, mirrorImage: true };
const data = await this.camera.takePictureAsync(options)
this.setState({path: data.uri})
}
}
takePicture is the function to capture the image. Now I want to save the captured image and upload it to Firebase storage.
this.setState({path: data.uri}) updates the path to clicked Image's location.
How do I use react native fetch blob to upload my image in firebase storage? Please help
I'm currently using react-native-camera - ^1.6.3 and firebase - ^5.0.3
回答1:
You can to using this sample code to upload your picture to the specific api.
var data = new FormData();
data.append('file', { uri: data.uri, name: 'picture.jpg', type: 'image/jpg' });
// Create the config object for the POST
const config = {
method: 'POST',
body: data
};
fetch('URL', config).then(responseData => {
// Log the response form the server // Here we get what we sent to Postman
back
console.log(responseData);
})
.catch(err => { console.log(err); });
Hope this help you.
回答2:
async function uploadFile() {
const data = new FormData();
data.append('file', { uri: data.uri, name: 'anyname.jpg', type: 'image/jpg' });
const response = await axios.post('URL', data);
const { id, url } = response.data;
}
It Dit worked, Thanks.
来源:https://stackoverflow.com/questions/53864677/how-do-i-upload-an-image-taken-by-react-native-camera-to-firebase-storage