如何使用react-native和firebase上传离线图片?

时间:2019-10-29 13:40:28

标签: javascript firebase firebase-storage

如何使用react-native和firebase上传离线图片?

我可以上传到Internet,URI是文件的本地链接,一旦连接恢复,我可以采取什么解决方案?

handleUploadImage = (uri, imagename) => {
console.log("Carregado:" + uri);

const image = uri;
const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

let uploadBlob = null;

const imageRef = firebase
  .storage()
  .ref("posts")
  .child(`fotoCasa${Date.now()}.jpg`);
let mime = "image/jpg";
fs.readFile(image, "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 => {
    // URL of the image uploaded on Firebase storage
    console.log(" URL GERADA: " + url);
    this.setState({ urlImagem: url });
    this.setState({ _takedPhotoUri: uri });
  })
  .catch(error => {
    console.log(error);
  });

};

0 个答案:

没有答案