如何在react-native android app

时间:2017-03-04 06:33:08

标签: android react-native react-native-android

我有一个base64图像,想要将其上传到服务器。我使用下面的代码通过fetch API将其发送到服务器。我首先使用解码base64格式将其转换为Blob对象。

let data = new FormData()
let converted = decodeBase64Image(signature)
data.append('image', converted)
data.append('visit_record_id', visitId);
fetch(Config.API_URL, {
    method: 'post',
    headers: {
      'Accept': 'application/json',
      'ACCESS_TOKEN': this.props.token,
      'Content-Type': 'multipart/form-data'
    },
    body: data
  }
).then(v => {
  console.log('response ', v);
})

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

运行上面的代码后,我得到以下错误:

    Possible Unhandled Promise Rejection (id: 0):
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.
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 compliantchain

有谁知道如何解决这个问题?

0 个答案:

没有答案