Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
903 views
in Technique[技术] by (71.8m points)

json - react-native-fetch-blob error RNFetchBlob multipart request builder has found a field without 'data'

I have try an upload image for react native using react-native-image-picker.

react-native-image-picker fine as well i cac pick an images from my emulator. but wheren i combine it with react-native-fetch=blob give me the error

"RNFetchBlob multipart request builder has found a fiel without data ... "

from the error, i figure the source problem is sent data null.

rewriting the code and find solution in internet, the syntax always same.

setting.js

constructor(props) {
    super(props);
    this.state = {
      hours: 17,
      minutes: 0,
      modalPassword: false,
      modalNotification: false,
      modalUpload: false,
      modalSuccess: false,
      password: '',
      oldPassword: '',
      userLogin: '',
      avatarSource: require('../assets/img/v41.png'),
      pic: null,
    };
    this.segmentedPicker = React.createRef();
    this._responseList();
  }

  myfun = () => {
    //alert('clicked');

    ImagePicker.launchImageLibrary(options, (response) => {
      if (response.didCancel) {
        console.log('User cancelled image picker');
      } else if (response.error) {
        console.log('Image Picker Error: ', response.error);
      } else {
        let source = {uri: response.uri};

        this.setState({
          avatarSource: source,
          pic: response.data,
        });
      }
    });
  };

and here is the handle uploading

uploadPic = () => {
    RNFetchBlob.fetch(
      'post',
      'https://att.vector41.org/changephoto.php',
      {
        Authorization: 'Bearer access-token',
        otherHeader: 'foo',
        'Content-Type': 'multipart/form-data',
      },
      [
        {
          name: 'image',
          filename: 'image.png',
          type: 'image/png',
          data: this.state.pic,
        },
      ],
    )
      .then((resp) => {
        alert(resp.data);
        this.setState({avatarSource: null});
      })
      .catch((err) => {
        alert('error');
      });
  };

my APi

<?php

$target_dir = "../portal/test_upload/";

if (!file_exists($target_dir)) {
  mkdir($target_dir, 0777, true);
}

$target_dir = $target_dir . "/" . rand() . '_' . time() . ".jpg";

if (move_uploaded_file($_FILES['image']['tmp_name'], $target_dir)) {
  echo json_encode([
    "Message" => "The File Has Been Uploaded!",
    "Status" => "Ok"
  ]);
} else {
  echo json_encode([
    "Message" => "Sorry, There was an error uploading your file!",
    "Status" => "Error"
  ]);
}

trying solve the problem

data: this.state.pic,

the data property who give by null data, i try transform it into string

String(this.state.pic)

but it give me another error again.

hey, i try this

data: 'this.state.pic',

the file is created. but the problem is the image is error and the image choose before not move to the folder.

the size is 9kb and cant even open it!

thank you, hope you all can help me...

question from:https://stackoverflow.com/questions/65935482/react-native-fetch-blob-error-rnfetchblob-multipart-request-builder-has-found-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...