skip to Main Content

Maybe anyone know how to send request like this ?

{ 'NewDocuments':[ 
 { 
 'id':1,
 'document': (my image)
 }
]}

i need inside Json object send image (not in base64, but as file),

  data.files.add(MapEntry(
        'document',
        MultipartFile.fromBytes(
          bytes,
          filename: fileName,
          contentType: mediaType,
        )));

if i send like this is okey, but when it inside "NewDocuments" it is the problem. Maybe you have encountered such problem?
I use Dio package to send request.

3

Answers


  1. Why do you want to send files in JSON? This is not the most suitable format for such data, you should think about other ways to send the document. Perhaps if you describe your needs better, we can give you some advice.

    Login or Signup to reply.
  2. Have you tried this?

    data.files.add(MapEntry("NewDocuments", [
        MapEntry("id", 1),
        MapEntry(
            'document',
            MultipartFile.fromBytes(
              bytes,
              filename: fileName,
              contentType: mediaType,
            ))
      ]));
    

    Logically, this should work. Try it out

    Login or Signup to reply.
  3. If you want to upload files using a JSON, a multipart request is not the way to go: multiparts exists to upload files, not JSONs.

    To actually send your file in a JSON, the better way to do so is to convert your file into a base64 string, essentially sending a stringified version of the document.

    As an example, you can do as it follows

    final String base64Doc = base64Encode(await doc.readAsBytes());
    

    and convert each file into base64 string, put them into your JSON and send it in a POST request (I assume).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search