简体   繁体   English

如何将用户从 ImagePicker.launchCameraAsync() 拍摄的图像 append 发送到 FormData

[英]how to append the image the user has taken from ImagePicker.launchCameraAsync() to a FormData

I am trying to append the photo taken by the user from ImagePicker.launchCameraAsync()我正在尝试 append 用户从 ImagePicker.launchCameraAsync() 拍摄的照片

I need to send the file itself not a base64 version of it and I am using expo with react native so react-native-fs doesn't work with me我需要发送文件本身,而不是它的 base64 版本,我正在使用 expo 和 react native,所以 react-native-fs 对我不起作用

I can get the path of the taken photo and the some other properties of it but not the image itself我可以获得拍摄照片的路径及其其他一些属性,但不能获取图像本身

A snippet of the code I am using to get the image is:我用来获取图像的代码片段是:

let permission = await ImagePicker.requestCameraPermissionsAsync();
    if (permission.granted === false) {
      alert("Camera access is required for single upload");
      return;
    }
    const takenImage = await ImagePicker.launchCameraAsync();
    if (takenImage.cancelled === true) {
      alert("No images were taken");
      return;
    }

    let data = new FormData();

And the request is要求是

await axios
      .post("A link here", data, {
        withCredentials: true,
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then((res) => {
        console.log(res.data);
      })
      .catch((err) => console.log(err.response));

So any ideas how can i do this trick?那么任何想法我怎样才能做到这一点?

you can use this code to append image to form data您可以使用此代码将 append 图像形成数据

let formdata = new FormData();
formdata.append('Name of the key from backend', {
    uri: 'file path of image',
    type: 'image type e.g jpeg/png etc',
    name: 'image name',
    });

After that send request like this之后像这样发送请求

let axiosConfig = {headers:{}}
axios.post(URL, formdata, axiosConfig)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM