如何从react native发布图像到django

xdyibdwo  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(104)

我一直试图从react-native发布一个图像文件,但似乎无法正确获取
这是我尝试的代码:

const pickImage = async () => {
        // No permissions request is necessary for launching the image library
        let result = await ImagePicker.launchImageLibraryAsync({
            mediaTypes: ImagePicker.MediaTypeOptions.Images, 
            allowsEditing: true,
            aspect: [4, 3],
            quality: 1,
          });
      
          if (!result.canceled){
            delete result.cancelled;
            setImage(result.assets[0].uri)
          }
      };

字符串
这是fetch请求:

const handleSubmit = (name, description, image, email, phone) => {
        let uri = image.replace("///", "//")
        var myHeaders = new Headers();
        myHeaders.append("Authorization", `Bearer ${userInfo.access_token}`);
        myHeaders.append("Content-Type", "multipart/form-data");

        var formdata = new FormData();
        formdata.append("name", name);
        formdata.append("description", description);
        formdata.append("photo", uri, uri.split("/").pop());
        formdata.append("phone", phone);
        formdata.append("email", email);

        var requestOptions = {
            method: 'POST',
            headers: myHeaders,
            body: formdata,
            redirect: 'follow'
          };

        fetch(`${Url.base}/api/realtor/create-profile`, requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
    }


这是我经常遇到的错误:
{“photo”:[“提交的数据不是文件。请检查表单上的编码类型。"]}

ee7vknir

ee7vknir1#

文件对象应该有urinametype字段,类似于:

const formData = new FormData();
formData.append(
  'photo',
  {
    uri: <uri>,
    name: <name>,
    type: <type>
  }
);

字符串

相关问题