DEV Community

putu eka mulyana
putu eka mulyana

Posted on • Updated on

Cara Upload File Atau Image Ke Firebase Pada React Native

Getting Started

install firebase pada projek kamu

npm i firebase
Enter fullscreen mode Exit fullscreen mode

atau

yarn add firebase
Enter fullscreen mode Exit fullscreen mode

Tambahkan Firebase configuration pada projeck kamu
saya menyimpan Firebase configuration pada root/src/config/Firebase.js isi file Firebase configuration seperti ini :

import { initializeApp } from "firebase/app";

const firebaseConfig = {
  apiKey: "xxxxx",
  authDomain: "xxxxx",
  databaseURL: "xxxxxxx",
  projectId: "xxxxxxx",
  storageBucket: "xxxxxxx",
  messagingSenderId: "xxxxxxx",
  appId: "xxxxxxx",
  measurementId: "xxxxxxx"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

export default app;
Enter fullscreen mode Exit fullscreen mode

view button

<TouchableOpacity style={styles.btnUpload}>
    <Text style={styles.btnLabel}>Upload Photo</Text>
</TouchableOpacity>
Enter fullscreen mode Exit fullscreen mode

install image picker
saya menggunakan library react-native-image-picker

npm i react-native-image-picker
Enter fullscreen mode Exit fullscreen mode

atau

yarn add react-native-image-picker
Enter fullscreen mode Exit fullscreen mode

import react-native-image-picker pada file screen kamu, misalkan root/src/screens/UploadPhoto.js

import {launchImageLibrary} from 'react-native-image-picker';
Enter fullscreen mode Exit fullscreen mode

install react-native-uuid

npm i react-native-uuid
Enter fullscreen mode Exit fullscreen mode

atau

yarn add react-native-uuid
Enter fullscreen mode Exit fullscreen mode

import pada file screen kamu

import uuid from 'react-native-uuid';
Enter fullscreen mode Exit fullscreen mode

import firebase
jangan lupa import firebase juga

import React from "react";
import {
  StyleSheet,
  Text,
  TouchableOpacity
} from "react-native";

import uuid from "react-native-uuid";
import { launchImageLibrary } from "react-native-image-picker";
import {
  getStorage,
  ref,
  uploadBytes,
} from "firebase/storage";

import firebase from "../config/Firebase";
Enter fullscreen mode Exit fullscreen mode

Buat function upload photo

const uploadPhoto= async () =>{
    await launchImageLibrary({}, async (response) => {
        if (response.didCancel || response.error) {
            Alert.alert("oops, batal memilih foto.");
        } else {
            let uri = response.assets[0].uri;
            let fileName = response.assets[0].fileName;

            let storage = await getStorage(firebase);

            const rersult = await fetch(uri);
            const blob = await rersult.blob();

            uploadBytes(ref(storage, fileName), blob, {
            contentType: "image/jpeg",
            firebaseStorageDownloadTokens: uuid.v4(),
            }).then(() => {
            console.log("Upload With blob");
            });
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

tambahkan onPress pada button

<TouchableOpacity style={styles.btnUpload} onPress={()=> uploadPhoto()}>
    <Text style={styles.btnLabel}>Upload Photo</Text>
</TouchableOpacity>
Enter fullscreen mode Exit fullscreen mode

Selesai 🚀😎

Top comments (0)