Convert image path to blob react native

后端 未结 4 1939
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 04:21

Problem

I am trying to create an app with react native and firebase. One of the features I would like for this app is the ability to upload images. I

4条回答
  •  广开言路
    2021-02-19 05:00

    Refer this link - https://github.com/dailydrip/react-native-firebase-storage/blob/master/src/App.js#L43-L69

    Following block of code is working fine.

    uploadImage(uri, mime = 'application/octet-stream') {
        return new Promise((resolve, reject) => {
          const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri
          let uploadBlob = null
    
          const imageRef = FirebaseClient.storage().ref('images').child('image_001')
    
          fs.readFile(uploadUri, 'base64')
            .then((data) => {
              return Blob.build(data, { type: `${mime};BASE64` })
            })
            .then((blob) => {
              uploadBlob = blob
              return imageRef.put(blob, { contentType: mime })
            })
            .then(() => {
              uploadBlob.close()
              return imageRef.getDownloadURL()
            })
            .then((url) => {
              resolve(url)
            })
            .catch((error) => {
              reject(error)
          })
        })
      }
    

提交回复
热议问题