2

Files/fields be like :

jsonData,

List[images],

MultipartFile,

sending all these fields/files at once to server using DIO

1
  • 1
    read dio's official documentation, they show how to do that Commented May 16, 2020 at 12:23

3 Answers 3

8

resolved .. by refering to https://github.com/flutterchina/dio#sending-formdata

Multiple files upload

There are two ways to add multiple files to FormData, the only difference is that upload keys are different for array types。

  FormData.fromMap({
    "files": [
      MultipartFile.fromFileSync("./example/upload.txt",
          filename: "upload.txt"),
      MultipartFile.fromFileSync("./example/upload.txt",
          filename: "upload.txt"),
    ]
  });

The upload key eventually becomes "files[]",This is because many back-end services add a middle bracket to key when they get an array of files. If you don't want “[]”,you should create FormData as follows(Don't use FormData.fromMap):

  var formData = FormData();
  formData.files.addAll([
    MapEntry(
      "files",
       MultipartFile.fromFileSync("./example/upload.txt",
          filename: "upload.txt"),
    ),
    MapEntry(
      "files",
      MultipartFile.fromFileSync("./example/upload.txt",
          filename: "upload.txt"),
    ),
  ]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you my man @ajs.sonawane, I knew this was the issue but just wasn't able to find the solution. Thanks, man I was finding the solution for the last 3 days. God bless you
How can you achieve this in http library?
How to retries the list of images from php side though?
2
"files": [
      await MultipartFile.fromFile("./text1.txt", filename: "text1.txt"),
      await MultipartFile.fromFile("./text2.txt", filename: "text2.txt"),
    ]

try add [] in "files[]": es:

"files[]": [
      await MultipartFile.fromFile("./text1.txt", filename: "text1.txt"),
      await MultipartFile.fromFile("./text2.txt", filename: "text2.txt"),
    ]

Comments

1

From documents of dio:

"Uploading multiple files to server by FormData:"

FormData.fromMap({
    "name": "wendux",
    "age": 25,
    "file": await MultipartFile.fromFile("./text.txt",filename: "upload.txt"),
    "files": [
      await MultipartFile.fromFile("./text1.txt", filename: "text1.txt"),
      await MultipartFile.fromFile("./text2.txt", filename: "text2.txt"),
    ]
});
response = await dio.post("/info", data: formData);

1 Comment

Yes, I'm doing the same way , but the List<MultipartFile> multipartfile is not uploading to server. and when tried multipartfile.elementAt(0) , it's taking .. not understanding what is the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.