【问题标题】:React Native + Firebase Storage: base64 and blob not workingReact Native + Firebase 存储:base64 和 blob 不起作用
【发布时间】:2017-09-16 19:33:08
【问题描述】:

我正在尝试将我的图像文件编码为 base64 或 blob,但它们都不起作用。我也在使用这个:https://github.com/react-community/react-native-image-picker 来管理图像选择器。

尝试 1:使用图像选择器的 .data 方法,该方法应该返回一个 base64 字符串

const blah = uuid.v4();
firebase
  .storage()
  .ref('images')
  .child(`${blah}.jpg`)
  .putString(file.data, 'base64', { contentType: 'image/jpeg' })

但这会抛出:Firebase Storage: String does not match format 'base64': Invalid character found

尝试 2:使用 https://github.com/wkh237/react-native-fetch-blob 的 Blob

      const uri = file.uri;
      const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri
      data = RNFetchBlob.fs.readFile(uploadUri, 'base64')
      blob = RNFetchBlob.polyfill.Blob.build(data, { type: 'application/octet-stream;BASE64' })
      const uniqueId = uuid.v4();
      const firebaseRef = firebase.storage().ref('images').child(`${uniqueId}.jpg`)
      return Observable.fromPromise(firebaseRef.put(blob, { contentType: 'application/octet-stream;BASE64' }));
    })

但是,它正在返回:Firebase Storage: Invalid argument inputat index 0: Expected Blob or File.

尝试 3:base64 使用 https://github.com/wkh237/react-native-fetch-blob

      const uri = file.uri;
      const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri
      data = RNFetchBlob.fs.readFile(uploadUri, 'base64')
      const uniqueId = uuid.v4();
      const firebaseRef = firebase.storage().ref('images').child(`${uniqueId}.jpg`)
      const metadata= {
        contentType: 'image/jpeg',
      };
      firebaseRef.putString(data, 'base64', metadata);

但这又会抛出:Firebase Storage: String does not match format 'base64': Invalid character found

有谁知道我做错了什么?

【问题讨论】:

  • 我遇到了同样的问题。真正奇怪的是 .data 在打开 react native 调试器时工作

标签: firebase react-native firebase-storage


【解决方案1】:

在我最近使用 5.5.6 版的那一天,我更新到 6.2.0 版并遇到了同样的问题。

const uri = Platform.OS === 'ios' ? response.uri.replace('file://', '') : response.uri
const imageRef = storage().ref('images/').child("blah")
await imageRef.put(uri)
const imageUrl = await imageRef.getDownloadURL()

我刚刚将.put 更改为.putFile,一切都恢复正常了

【讨论】:

    【解决方案2】:

    关于(尝试1)你必须使用'data_url':

    .putString(file.data, 'data_url', { contentType: 'image/jpeg' })
    

    【讨论】:

    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 2018-12-25
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多