【问题标题】:react native app is crashing with no logs when uploading image on ios在 ios 上上传图像时,本机应用程序崩溃且没有日志
【发布时间】:2022-08-12 09:23:34
【问题描述】:

当我尝试在 android 上使用 firebase uploadBytes 时,它可以正常工作,但在 ios 上,只要视频文件大于 2 mb,应用程序就会崩溃且没有日志 我尝试使用uploadBytesResumable,甚至尝试将我的firebase版本降级到版本8,但还没有解决方案

const fetchImage = await fetch(localUri);
const imageBlob = await fetchImage.blob();
const storage = getStorage();
const upload = ref(storage, \'path_to_sotrage\');
const uploadTask = await uploadBytesResumable(upload, imageBlob);
return await getDownloadURL(uploadTask.ref);
  • uploadBytesResumable() 不会返回 Promise,因此 await 在那里是不必要的。您是否尝试过使用 uploadBytes() 代替?
  • 你找到解决方案了吗?在这里面临同样的问题 & android 崩溃甚至没有上传。

标签: javascript firebase react-native expo firebase-storage


【解决方案1】:

这个uploadBytes 对我来说崩溃了,我不知道为什么。但是,documentation 中的 uploadBytesResumable 模式正在运行并且看起来很稳定。希望能帮助到你。

import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";

const storage = getStorage();

// Create the file metadata
/** @type {any} */
const metadata = {
  contentType: 'image/jpeg'
};

// Upload file and metadata to the object 'images/mountains.jpg'
const storageRef = ref(storage, 'images/' + file.name);
const uploadTask = uploadBytesResumable(storageRef, file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on('state_changed',
  (snapshot) => {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case 'paused':
        console.log('Upload is paused');
        break;
      case 'running':
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
    switch (error.code) {
      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;
      case 'storage/canceled':
        // User canceled the upload
        break;

      // ...

      case 'storage/unknown':
        // Unknown error occurred, inspect error.serverResponse
        break;
    }
  }, 
  () => {
    // Upload completed successfully, now we can get the download URL
    getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2012-11-15
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多