0

I got the errors failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type' Blob'.My library is 0.10.8 version. I have tried to downgrade react native fetch blob to 0.9.5 but i got another error.

listeners is not defined

i have read problem at github, but i don't really understand with their solutions i confused right now.

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)

})

})

}
Kaiido
  • 123,334
  • 13
  • 219
  • 285
user8784065
  • 39
  • 2
  • 6
  • What is `Blob.build()`? – guest271314 Oct 20 '17 at 01:04
  • And where is the call to `FileReader#readAsText` made? @guest271314 they declare `Blob` as a *polyfill* at top of code block (was out of code-block formatting) – Kaiido Oct 20 '17 at 01:10
  • I just learning from my friends github sir, but his project work fine. but i i got errors. What's wrong here sir? i don't understand about it – user8784065 Oct 20 '17 at 01:33

1 Answers1

0

I solved it by resetting the window.XMLHttpRequest to it's original state after finished using the fetch blob for uploading my image to cloud. Hope it helps :)

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));
    });
});
barbsan
  • 3,418
  • 11
  • 21
  • 28
Jaz
  • 1
  • 2