原因是两种不同for循环导致的。
在Flutter 为了方便大部分都会使用forEach或者map的,结果就是这种方式导致没有拿到文件。
服务端获取不到文件写法
view?.showLoadingProgress();
//就是使用map导致的
List<Future<MultipartFile>> files = paths.map((path) async {
String filename = path.substring(path.lastIndexOf("/") + 1);
return await MultipartFile.fromFile(path, filename: filename);
}).toList();
FormData formData = new FormData.fromMap({"file": files});
DioHolder().call(
MainApiConstant.UPLOAD,
method: DioHolder.POST,
data: formData,
onSuccess: (map) {
view?.onSubmitSuccess();
view?.dismissLoadingProgress();
},
onError: (NetError e) {
view!.onToast(e);
view?.dismissLoadingProgress();
},
);
});
修改后写法(解决)
view?.showLoadingProgress();
List<MultipartFile> files = [];
for (var file in imageFiles) {
String filename = file.path.substring(file.path.lastIndexOf("/") + 1);
var suffix = filename.substring(filename.lastIndexOf(".") + 1);
MultipartFile multipartFile = await MultipartFile.fromFile(
file.path,
filename: "filename.$suffix",
contentType: MediaType.parse("image/$suffix"),
);
files.add(multipartFile);
}
FormData formData = FormData.fromMap({"files": files});
DioHolder().call(
MainApiConstant.UPLOAD,
method: DioHolder.POST,
data: formData,
options: Options(
contentType: "multipart/form-data",
),
onSuccess: (map) {
view?.onSubmitSuccess();
view?.dismissLoadingProgress();
},
onError: (NetError e) {
view!.onToast(e);
view?.dismissLoadingProgress();
},
);
});
网友评论