文件/字段类似:
jsonData,
List[images],
MultipartFile,
使用DIO一次将所有这些字段/文件发送到服务器
答案 0 :(得分:1)
摘自dio的文档:
“通过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 :(得分:1)
通过引用https://github.com/flutterchina/dio#sending-formdata
解决。上传多个文件
有两种向FormData添加多个文件的方法,唯一的区别是数组类型的上载键不同。
FormData.fromMap({
"files": [
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
]
});
上载密钥最终变为“ files []”,这是因为许多后端服务在获取文件数组时在密钥中添加了中间括号。如果不想使用[[]],则应按以下方式创建FormData(不要使用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"),
),
]);