通过post方法反应本地上传图像

时间:2020-08-27 13:00:06

标签: react-native react-redux axios fetch form-data

我在将图像上传到服务器时遇到问题,我的服务器使用表单数据,并且我还有其他字段,我想通过邮寄方式发送数据,但是我不能帮我????

formPoster = () => {
    let body = new FormData();
    let formData = new FormData();
    formData.append('nationalId', this.state.nationalId);
    formData.append('name',this.state.name);
    formData.append('lastName',this.state.lastName);
    formData.append('mobileNumber',this.state.mobileNumber);
    formData.append('password',this.state.password);
    var fileName = this.state.image.split('/').pop();
    var fileType = this.state.image.split('.').pop();
    var filePath =  'file://' + this.state.image;

    formData.append('file[]', {
      uri: this.state.image,
      name: fileName,
      type: fileType,
 });

 fetch("sample.myserver.......",
  {
    body: formData,
    method: "post"
  });

 }

2 个答案:

答案 0 :(得分:0)

获取图像的位置,格式应为

let formData = new FormData();
images.map((element: any, index: number) => {
                            const imageUrl: any = {
                                uri: element.uri,
                                type: element.mime,
                                name: `whatever`,
                            };
                            formData.append('imageUrls', imageUrl, `whatever`);
                        });

答案 1 :(得分:0)

定义的文件类型不正确,应从file.mime中获取