[英]failed to execute 'readAsText' on 'FileReader' :parameter 1 is not of type 'Blob'
I got the errors failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type' Blob'.My library is 0.10.8 version. 我遇到了无法在'FileReader'上执行'readAsText'的错误:参数1不是'Blob'类型。我的库是0.10.8版本。 I have tried to downgrade react native fetch blob to 0.9.5 but i got another error.
我曾尝试将原生抓取Blob降级到0.9.5,但又遇到另一个错误。
listeners is not defined
未定义侦听器
i have read problem at github, but i don't really understand with their solutions i confused right now. 我在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)
})
})
}
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. 我完成了使用fetch blob将图像上传到云后,通过将
window.XMLHttpRequest
重置window.XMLHttpRequest
原始状态来解决该问题。 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)); }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.