[Django]-How do I append a list to FormData?

9👍

You can send a list through FormData this way:

postForm.customList.forEach(item => {
 formData.append('custom_list', item);
});

This will give you a custom_list array on the backend side.

0👍

Get file from your input, you can use ref

const fileRef = useRef(null)

or

const fileRef = React.createRef()
<input 
  type='file'
  label='Upload'
  accept='image/*' 
  ref={fileRef}
/>
const theFile = fileRef.files[0]

then you can simply use this function

function getBase64String(file, cb) {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function () {
        cb(reader.result)
    };
    reader.onerror = function (error) {
        console.log('Error: ', error);
    };
}
let myImageStringToSendInJson = '';
getBase64String(theFile, (result) => {
     myImageStringToSendInJson = result;
})

get base64 string and then simply send that base64 string in json
Note : you do not need to specify any other header in axios or fetch, if you are sending base64

Leave a comment