如何在react-native中使用fetch发布multipart/formdata? [英] How to post multipart/formdata using fetch in react-native?

查看:317
本文介绍了如何在react-native中使用fetch发布multipart/formdata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样发布表单数据.

i want to post Form Data like that.

我应该为发送图像文件数据做些什么?

what should i prepare for sending image file data?

我有Uri,类型,文件名,大小.

i have Uri, type, filename, size.

然后将使用访存.

标题中的内容类型为'multipart/formdata'

Content-type in header is 'multipart/formdata'

感谢您的帮助

推荐答案

您应该具有上传功能,该功能应如下所示:

You should have an upload function, which should look like this:

upload(url, data) {
  let options = {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    method: 'POST'
  };

  options.body = new FormData();
  for (let key in data) {
    options.body.append(key, data[key]);
  }

  return fetch(requestUrl, options)
      .then(response => {
        return response.json()
          .then(responseJson => {
            //You put some checks here
            return responseJson;
          });
      });
}

您这样称呼,发送图像Blob路径:

And you call it this way, sending the image blob path:

this.upload('http://exampleurl.com/someApiCall', {
  file: {
    uri: image.path,
    type: image.mime,
    name: image.name,
  }
}).then(r => {
  //do something with `r`
});

这篇关于如何在react-native中使用fetch发布multipart/formdata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆