简体   繁体   中英

How to send data in api without losing it

Below code i'm adding objects to media array in reactJS

const formData = new FormData()
formData.append('file', file, file.name)
const obj = {
  'id': id,
  'type': 'doc',
  'data': formData,
  url: file
};
setID(id + 1);
setMedia([...media, obj]);

Now i send it to api via axios

function addNew() {
  setProgressing(true)
  axios.post(uri + '/api/add', {
      map_id: props.data.id,
      questions: JSON.stringify(media)
    })
    .then(res => {
      props.updateStep();
    })
    .catch(err => {

    });
}

But on server end i receive object of array like below see data and url is empty

{id: 1, type: "image", data: {{}}, url: {}}

this is request header

在此处输入图像描述

SetMedia code which call on each image upload

 function handleUploadChange(e) {
        handleClose();
        const file = e.target.files[0];
        if (!file) {
            return;
        }
        const reader = new FileReader();
        reader.readAsBinaryString(file);
        reader.onload = () => {
            const formData = new FormData()
            formData.append('file', file, file.name)

            const obj = { 'id': id, 'type': 'image', 'data': formData, url: file };
            setID(id+1);
            setMedia([...media, obj]);


        }


        reader.onerror = function () {
            console.log("error on load image");
        };
    }

i find solution for it.

i convert images in base64 and send string in request and on server side convert it again to images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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