Thuta Learning
Firebase Tutorial
AdvancedDevOps & Toolsအလယ်အလတ်

File Upload တင်ခြင်း

Firebase Tutorialသင်ခန်းစာ 21

File Upload တင်ခြင်း

File upload လုပ်ဖို့ Storage reference တစ်ခုတည်ဆောက်ပြီး uploadBytes ကိုသုံးပါတယ်။ File path ကို user ID နဲ့ခွဲထားရင် နောက်ပိုင်း manage လုပ်ရလွယ်ပါတယ်။

Code Example

javascript
import { getStorage, ref, uploadBytes } from 'firebase/storage';
import { getAuth } from 'firebase/auth';

const storage = getStorage();
const auth = getAuth();

async function uploadAvatar(file) {
  const user = auth.currentUser;
  if (!user) throw new Error('Login required');

  const filePath = 'avatars/' + user.uid + '/' + file.name;
  const fileRef = ref(storage, filePath);

  await uploadBytes(fileRef, file);

  console.log('Avatar uploaded:', filePath);
}

ဒီ code က ဘာလုပ်တာလဲ?

avatars/userId/fileName ပုံစံနဲ့ path တည်ဆောက်ပြီး Storage ထဲကို file တင်ပါတယ်။ User ID ပါထားလို့ user တစ်ယောက်ချင်းစီရဲ့ files တွေကိုခွဲခြားလို့ရပါတယ်။

Common mistake

User ကတင်တဲ့ file.name တူနေရင် overwrite ဖြစ်နိုင်ပါတယ်။ Production မှာ timestamp သို့မဟုတ် random ID ထည့်ပြီး filename unique ဖြစ်အောင်လုပ်ပါ။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

File Upload တင်ခြင်း | Thuta Learning