当我点击一个api时,我想以flutter的形式上传一个多部分/Formdata格式的图像和配置文件数据



这是我的post-api代码,当我运行代码时,我试图上传文件(来自图像选择器的图像(和profilePojo(用户名、fname、姓氏等数据(,但结果失败了''

void addData(final profilePojo) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
String token = preferences.getString('token');
FormData formData = FormData.fromMap({
"file": await MultipartFile.fromFile("./text.txt",filename: "upload.txt"),

"profilePojo":profilePojo,//profilePojo means i pass heaar string of data on button click
});

String url =pass here url
http
.post(
url,
headers: {
HttpHeaders.authorizationHeader: 'Bearer $token',
// "Content-Type":"multipart/form-data",
"accept": "application/json",
},
body: formData.toString()

)
.then((response) {
if (response.statusCode == 200) {
var myData = json.decode(response.body);

if(myData['result']=="success"){
setState(() {
print(myData);//print response  success
_showDialog();
getData();
});}
else{
print(response.statusCode);

print(myData);
}
} else {
print(response.statusCode);
print("object");
}
});
}

''

我目前正在使用dio处理此类请求,下面是我的示例:

final futureUploadList = imageList.map((img) async {
print(img.path);
return MultipartFile.fromFile(img.path);
});
final uploadList = await Future.wait(futureUploadList);
FormData data = FormData.fromMap({
"images": uploadList
});
dio.post('/images',
data: data, options: Options(headers: {'Authorization': 'Bearer abcd'}));

最新更新