在 React 本机应用程序上得到“分配的来源之一在原型链上有一个可枚举的键”

2024-02-17

我正在为Android应用程序使用react-native。并使用axios作为http库。当我尝试发送Blob通过 http post 对象我会得到以下错误:

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

问题的根源在于 React Native 开发人员进行了不符合规范的性能优化(这就是为什么代码可以在您的网站上运行,但不能在您的 React Native 应用程序上运行)。有关更多详细信息,请参阅我在此处打开的问题:https://github.com/facebook/react-native/issues/16814 https://github.com/facebook/react-native/issues/16814

作为解决方法,您可以使用反应本机获取 blob https://github.com/wkh237/react-native-fetch-blob#multipartform-data-example-post-form-data-with-file-and-data。我遇到了与您相同的错误,react-native-fetch-blob 为我解决了这个问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 React 本机应用程序上得到“分配的来源之一在原型链上有一个可枚举的键” 的相关文章

随机推荐