【问题标题】:Upload Image from React Native using react-native-image-picker to Amazon S3 with presigned url使用 react-native-image-picker 将图像从 React Native 上传到带有预签名 url 的 Amazon S3
【发布时间】:2018-08-25 15:43:04
【问题描述】:

我的案子需要帮助。 我正在使用 react-native-image-picker 来挑选图片,目前的问题是何时需要上传图片。

我准备了presignedurl

//here I get the presigned url
  const uploadConfig = await profileApi.getUpdateUrl(id); 
  //I prepare file from response of imagepicker : uri , filename , file type, data(base64 file)
  const file = {
    uri: values.Avatar.uri,
    name: values.Avatar.fileName,
    type: values.Avatar.type,
    data: value.Avatar.data,
  }; 

  //upload to presignedurl of S3 , already tried file.data , file.uri respectively but it resulted same
  const resultUpload = await axios.put(uploadConfig.data.url, file, {
    headers: {
      'Content-Type': file.type,
    },
  });

结果被接受,没有错误,但结果是空白图像,当我下载时不是图像。

【问题讨论】:

    标签: reactjs react-native amazon-s3 image-upload


    【解决方案1】:

    不要使用 axios 而是使用 XMLHttpRequest,我不知道为什么,但这有效:

    const xhr = new XMLHttpRequest()
    xhr.open('PUT', presignedUrl)
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                console.log('Image successfully uploaded to S3')
            } else {
                console.log('Error while sending the image to S3')
            }
        }
    }
    xhr.setRequestHeader('Content-Type', response.type)
    xhr.send({uri: response.uri, type: response.type, name: response.filename})
    

    【讨论】:

    • 天哪,你拯救了我的一天!但我使用的是 axios,它发送的是 JSON 而不是文件。我试过这个,它对我有用。但是,为什么 axios 不起作用?
    • 嗨,很高兴听到这对您有所帮助,我真的不知道为什么,但我认为这与 FormData check this
    猜你喜欢
    • 2020-09-16
    • 2020-03-13
    • 2016-12-08
    • 2021-03-24
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2015-08-24
    • 2021-10-20
    相关资源
    最近更新 更多