端点的签名是:
public async Task<ActionResult> Post([FromForm]List<IFormFile> imgFile)
与Postman尝试过,效果很好。
此代码有效:
List<String> ss = partInfo.rejectionPicturesPath;
FormData formData = FormData.fromMap({
"imgFile": await MultipartFile.fromFile(ss[0]),
});
现在的问题是如何编写它,以便可以将其作为列表发送...
// Don't work
List<FormData> formData = [];
await Future.forEach(partInfo.rejectionPicturesPath, (p) async {
formData.add(FormData.fromMap({
"imgFile" : await MultipartFile.fromFile(p),
}));
});
// Don't work
List<String> ss = partInfo.rejectionPicturesPath;
FormData formData = FormData.fromMap({
"imgFile": await MultipartFile.fromFile(ss[0]),
"imgFile": await MultipartFile.fromFile(ss[1]),
});
// Don't work either
FormData formData = FormData.fromMap({
"imgFile": [
await MultipartFile.fromFile(ss[0]),
await MultipartFile.fromFile(ss[1]),
]
});
我以这种方式发送:
BaseOptions options = BaseOptions(
contentType: "multipart/form-data"
);
var dio = Dio(options);
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
// I'm stuck here trying to build a list of the images, see non working codes above
var resp = await dio.post(baseUrl, data: data);
如何发送图像列表?我很确定这是可以做到的,因为Postman可以很好地发布它。如何将其写到Dart的代码中?
答案 0 :(得分:0)
设法使它起作用:
List<String> ss = partInfo.rejectionPicturesPath;
var data = FormData();
data.files.addAll([
MapEntry(
"imgFile",
await MultipartFile.fromFile(ss[0]),
),
MapEntry(
"imgFile",
await MultipartFile.fromFile(ss[1]),
),
]);