Reactjs 在发送内容类型多部分/表单数据时,如何将图像/jpeg文件上载到s3上预先指定的url?

Reactjs 在发送内容类型多部分/表单数据时,如何将图像/jpeg文件上载到s3上预先指定的url?,reactjs,react-native,http,amazon-s3,pre-signed-url,Reactjs,React Native,Http,Amazon S3,Pre Signed Url,我可能对这项工作有一些误解。基本上,我想上传图像到一个通过S3生成的预先签名的url。目前,我正在发送带有以下标题的POST请求: "Content-Type": 'image/jpeg', "file": { uri: some_image, type: 'image/jpeg', name: `some_image.jpeg` } 所以这是可行的。但是我发现上传速度非

我可能对这项工作有一些误解。基本上,我想上传图像到一个通过S3生成的预先签名的url。目前,我正在发送带有以下标题的POST请求:

      "Content-Type": 'image/jpeg',
      "file": {
        uri: some_image,
        type: 'image/jpeg',
        name: `some_image.jpeg`
      }
所以这是可行的。但是我发现上传速度非常慢。每个图像的速度为2-3 MBs。在wifi上要快得多,但在移动网络上,可能需要30秒。我已经读到,将内容类型更改为多部分/表单数据将有助于加快上传速度。但是,当我将其修改为
内容类型:表单数据
时,s3上的元数据将设置为
内容类型:表单数据
。但是我希望
内容类型:表单数据
图像/jpeg
,因为此图像将在互联网上提供。如果未设置为
内容类型:image/jpeg
,浏览器只需将文件下载到目录中即可

以下内容不起作用,因为S3上图像上的元数据具有元数据内容类型:multipart/form data

      "Content-Type": 'multipart/form-data',
      "file": {
        uri: some_image,
        type: 'image/jpeg',
        name: `some_image.jpeg`
      }
我在用React。我的axios函数如下所示

export default function HTTP({ method, url, data, params, headers }) {  
  return new Promise((resolve, reject) => {    
    const query = {
      method: method,
      url: url,
    }
    if (params) {
      query.params = params
    }
    if (headers != null) {
      query.headers = headers;
    }
    if (data) {
      query.data = data;
    }   
    axios({...query, timeout: 30000}).then(function (response) {
      resolve(response);
    })
      .catch((error) => {
        if (error) {
          NavigatorService.navigate('Back');
        }
        reject(error)

      })
  })
}

谢谢

您好@readdit55您需要像这样创建表单数据

Example:
       headers: 
           "Content-Type": 'multipart/form-data',
       body: 
          createFormData({
             'uri': some_image,
             'type': 'image/jpeg',
             'name'': `some_image.jpeg`
          });

**Method to create form data:**
       const createFormData = (objects) => {
             const data = new FormData();
             Object.keys(objects).forEach((key,Index)=>{
                 data.append(key, body[key]);
             });
             return data;
       })

在哪里创建表单数据?只要您提到“内容类型”:“多部分/表单数据”。您需要将文件对象转换为form-data.multipart,因为它是协议的类型。如果您向axios传递流,它将自动使用多部分协议技术,并且您可以保留预签名url“image/jpeg”的contentType