0

我得到错误无法在'FileReader'上执行'readAsText':参数1不是'Blob'类型。我的库是0.10.8版本。我试图将 react native fetch blob 降级到 0.9.5,但我遇到了另一个错误。

未定义侦听器

我在 github 上读过问题,但我不太了解他们的解决方案,我现在很困惑。

const Blob = RNFetchBlob.polyfill.Blob

const fs = RNFetchBlob.fs

window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest

window.Blob = Blob

​

const screenWidth = Dimensions.get('window').width

​

const uploadImage = (uri, imageName, mime = 'image/jpg') => {

return new Promise((resolve, reject) => {

const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri

let uploadBlob = null

const imageRef = firebaseApp.storage().ref('posts').child(imageName)

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)

})

})

}
4

1 回答 1

0

window.XMLHttpRequest在完成使用 fetch blob 将我的图像上传到云后,我通过将其重置为原始状态来解决它。希望能帮助到你 :)

const Blob = FetchBlob.polyfill.Blob;
// Keep an original copy of window.XMLHttpRequest before set it to FetchBlob.polyfill.XMLHttpRequest
const oriWindowXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = FetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

return new Promise((resolve, reject) => {
  Blob.build(imageFile, {
      type: mime
    })
    .then(blob => {
      uploadBlob = blob;
      return imageRef.put(blob, {
        contentType: mime
      });
    })
    .then(() => {
      uploadBlob.close();
      return imageRef.getDownloadURL();
    })
    .then(url => {
      // Reset it to the original WindowXMLHttpRequest after the task has been done completely
      window.XMLHttpRequest = oriWindowXMLHttpRequest;
      resolve(getSuccessResponse(url));
    })
    .catch(error => {
      // Reset it to the original WindowXMLHttpRequest after the task has been done completely
      window.XMLHttpRequest = oriWindowXMLHttpRequest;
      showToast(getErrorMessage(error));
      reject(getFailResponse(error));
    });
});

于 2018-11-13T09:43:08.010 回答