【问题标题】:How to upload a video to firebase firestore in React Native如何在 React Native 中将视频上传到 Firebase Firestore
【发布时间】:2020-08-16 13:30:47
【问题描述】:

我目前能够通过将图像存储在 blob 中并将其上传到 firestore 来将图像上传到 firebase firestore。我将如何做同样的事情,但对于视频,我也会将它放在一个 blob 中吗?下面是我用来上传图片的代码。

openLibrary = async () => {
 const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL)
 if (status === 'granted') {
 const image = await ImagePicker.launchImageLibraryAsync({allowsEditing: true})
 if(!image.cancelled){
 const url = await this.props.uploadPhoto(image)
 this.props.updatePhoto(url)
      }
    }
  }
export const uploadPhoto = (image) => {
 return async (dispatch) => {
 try {
 console.log("in upload photo")
 const resize = await ImageManipulator.manipulateAsync(image.uri, [], { format: 'jpeg', compress: 0.1 })
 const blob = await new Promise((resolve, reject) => {
 const xhr = new XMLHttpRequest()
 xhr.onload = () => resolve(xhr.response)
 xhr.responseType = 'blob'
 xhr.open('GET', resize.uri, true)
 xhr.send(null)
      });
 const uploadTask = await firebase.storage().ref().child(uuid.v4()).put(blob)
 const downloadURL = await uploadTask.ref.getDownloadURL()
 return downloadURL
    } catch(e) {
 console.log("in upload photo error")
 console.error(e)
    }
  }
}

【问题讨论】:

  • 从 Cloud Storage 上传和下载的所有内容都以相同的方式工作 - 文件类型不会受到特殊处理。它们只是字节序列。
  • 另请注意:您不是将视频上传到 Cloud Firestore(文档数据库),而是将其上传到 Cloud Storage(博客存储解决方案),然后将视频的 URL 存储在数据库。
  • Doug 和我的评论之间,问题似乎归结为:google.com/…
  • @FrankvanPuffelen 我设法上传了它,但是当我下载它以文本格式下载的文件时,如何以视频格式获取它?

标签: javascript firebase react-native google-cloud-firestore firebase-storage


【解决方案1】:

您是如何尝试下载文件的?

也许当您上传视频时,您没有在上传或下载(mp4、mov 等)时设置文件类型/扩展名。

尝试添加所需的文件扩展名,例如,请参阅this link

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2020-09-07
    • 2020-03-06
    • 2020-06-30
    • 2017-07-02
    • 2017-11-17
    • 2022-10-18
    • 1970-01-01
    相关资源
    最近更新 更多