DEV Community

putu eka mulyana
putu eka mulyana

Posted on β€’ Edited on

5 4

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 πŸš€πŸ˜Ž

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❀️