我正在为Android应用使用react-native。并使用axios
作为http库。当我尝试通过http帖子发送Blob
对象时,我将收到以下错误:
HTTP Failure in Axios TypeError: One of the sources for assign has an enumerable key on the prototype chain. Are you trying to assign a prototype property? We don't allow it, as this is an edge case that we do not support. This error is a performance optimization and not spec compliant.
下面是我用于在表单数据上添加blob对象的代码:
let data = new FormData()
data.append('image', decodeBase64Image(image));
下面是解码base64图像的代码。以下代码在我的一个网站应用程序中工作正常。
export const decodeBase64Image = (dataURI) => {
let byteString;
if (dataURI === undefined) {
return undefined
}
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
let mimeString = ''
if (dataURI.split(',')[0] != undefined && dataURI.split(',')[0].split(':')[1] != undefined) {
mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
}
// write the bytes of the string to a typed array
let ia = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type: mimeString});
}
答案 0 :(得分:0)
问题的根源在于React Native开发人员进行了不符合规范的性能优化(这就是代码在您的网站上运行的原因,而不是您的React Native应用程序)。有关详细信息,请参阅我在此处打开的问题:https://github.com/facebook/react-native/issues/16814
作为解决方法,您可以使用react-native-fetch-blob。我遇到了你做的同样的错误,并且react-native-fetch-blob为我解决了它。