Zha*_* Yi 5 android react-native react-native-android
我正在使用react-native for Android app.并axios用作http库.当我尝试Blob通过http帖子发送对象时,我将得到以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
下面是我用来在表单数据上添加blob对象的代码:
let data = new FormData()
data.append('image', decodeBase64Image(image));
Run Code Online (Sandbox Code Playgroud)
下面是解码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});
}
Run Code Online (Sandbox Code Playgroud)
问题的根源在于 React Native 开发人员进行了不符合规范的性能优化(这就是为什么代码可以在您的网站上运行,但不能在您的 React Native 应用程序上运行)。有关更多详细信息,请参阅我在此处打开的问题:https ://github.com/facebook/react-native/issues/16814
作为解决方法,您可以使用react-native-fetch-blob。我遇到了与您相同的错误,react-native-fetch-blob 为我解决了这个问题。
| 归档时间: |
|
| 查看次数: |
2097 次 |
| 最近记录: |