未能在'FileReader'上执行'readAsText':参数1不是'Blob'类型

时间:2017-10-20 00:54:21

标签: javascript firebase react-native react-native-fetch-blob

我得到的错误无法在'FileReader'上执行'readAsText':参数1不是'Blob'类型。我的库是0.10.8版本。我试图将本机提取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)

})

})

}

1 个答案:

答案 0 :(得分:0)

在通过使用获取blob将我的图像上传到云中完成后,我将window.XMLHttpRequest重置为其原始状态来解决了该问题。 希望对您有所帮助:)

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