meenu cd
meenu cd

Reputation: 37

append image with form data using axios patch giving error

i am trying to append image with form data the problem is that if image is not passed i get error submitted data is not file because image is passed as undefined

const formData = new FormData();
formData.append("image", image);//if image is empty dont append image with form data

Object.entries(Data).forEach(([key, value]) => formData.append(key, value)); 

axios.patch("url-for-api/", formData, config)

if the image is undefinnd axios patch is giving error

Upvotes: 0

Views: 188

Answers (2)

faraj shuaib
faraj shuaib

Reputation: 46

const formData = new FormData();
      
for (const [key, value] of Object.entries(values)) {
    if (value) {
      formData.append(key, value);
    }
}
axios.patch("url-for-api/", formData, config)

Upvotes: 1

meenu cd
meenu cd

Reputation: 37

const formData = new FormData(); if (image != undefined) {
      formData.append("prescription", image);
    }
Object.entries(Data).forEach(([key, value]) => formData.append(key, value)); 

axios.patch("url-for-api/", formData, config)

Upvotes: 0

Related Questions