React Native / Firebase - 上传包含其他信息的图片

时间:2017-08-04 06:46:15

标签: firebase react-native

当按下按钮上传到firebase时,我使用这段代码来选择图像。

    <TouchableHighlight onPress={ () => this._pickImage() }
    style={styles.button}>
    <Text
    style={styles.buttonText}>
    Select a Photo
    </Text>
    </TouchableHighlight>

    _pickImage() {
    this.setState({ uploadURL: '' })

    ImagePicker.launchImageLibrary({}, response  => {
                                   uploadImage(response.uri)
                                   .then(url => this.setState({ uploadURL: url }))
                                   .catch(error => console.log(error))
                                   });
    }// end _pickImage

const uploadImage = (uri, mime = 'application/octet-stream') => {
       return new Promise((resolve, reject) => {
                       const uploadUri = Platform.OS === 'ios' ?              uri.replace('file://', '') : uri
                    const sessionId = new Date().getTime()
                   let uploadBlob = null
                   const imageRef = storage.ref('images').child(`${sessionId}`)

                   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)
                          })
                   })
}

这很好用。如何更改此选项以让用户选择照片然后点击另一个按钮存储图像的其他信息,例如用户输入到firebase中相同数据结构中的描述?

0 个答案:

没有答案