0

i have an array with json objects. json objects cointains images i have to send the data in ajax call with json.stringify?

i have to send the data through ajax(array contains json objects contains image files) call the multipart image input file become null when we use JSON.stringfy but if we dont use it will be sent as an [object object ] file how can i send an image file without object

before json.stringify ==>

0: colorCode: "123" count: "213" imageMultipartFiles: File lastModified: 1660657238975 lastModifiedDate: Tue Aug 16 2022 16:40:38 GMT+0300 (Arabian Standard Time) {} name: "screencapture-localhost-8082-dibsy-user-payment- success-2022-08-16-16_40_34.png" size: 326173 type: "image/png" webkitRelativePath: "" [[Prototype]]: File name: "123" value: "123"


after json.stringify ===>

[{"name":"123","count":"213","value":"123","imageMultipartFiles":{},"colorCode":"123"}]

can anyone help in this scenario

Thanks in advance

1 Answers1

0

JSON doesn't have an "image" (or "file") data type so you'll need to encode the data in one of the types that are supported.

A string is usual. You could convert the image to base64 and include it that way.

You could use an object where one of the properties is the base64 data if you want to include any metadata.

This obviously depends on what format the server is expecting the data in.

You might want to consider using multi-part/form-data instead of JSON. It's designed for handling files as well as other kinds of data.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335