我正在尝试发布图像和数据,但是我得到此错误plz帮助。 我正在使用dio:^ 3.0.9。 在邮递员中工作得很好,而且如果我从数据中删除图像并仅发布数据,它就可以很好地工作。
class ProfileRepository {
LocalStorage localStorage = new LocalStorage();
Dio dio = new Dio();
final url = 'http://localhost:3000/api/profile';
Future<ProfileModel> getProfileResponse(
String fullName,
String position,
String gender,
String homeAddress,
String officeAddress,
String phoneNumber,
File image) async {
final token = await localStorage.getLoginToken();
final splitToken = token.split(' ');
final finalToken = splitToken[1];
String imageFile = image.path.split('/').last;
var formData = new FormData.fromMap({
"fullname": fullName,
"position": position,
"gender": gender,
"home_address": homeAddress,
"office_address": officeAddress,
"phone_no": phoneNumber,
"image": await MultipartFile.fromFile(image.path, filename: imageFile),
});
final response = await dio.post(url,
data: formData,
options: Options(headers: {'Authorization': finalToken}));
if (response.statusCode == 201) {
return ProfileModel.fromJson(json.jsonDecode(response.data));
} else {
throw Exception("Error");
}
}
}
答案 0 :(得分:0)
final url = 'http://localhost:3000/api/profile';
Future<ProfileModel> getProfileResponse(
String fullName,
String position,
String gender,
String homeAddress,
String officeAddress,
String phoneNumber,
File image) async {
final token = await localStorage.getLoginToken();
final splitToken = token.split(' ');
final finalToken = splitToken[1];
String imageFile = image.path.split('/').last;
FormData formData() {
var formData = new FormData.fromMap({
"fullname": fullName,
"position": position,
"gender": gender,
"home_address": homeAddress,
"office_address": officeAddress,
"phone_no": phoneNumber,
});
formData.files.addAll([
MapEntry(
"files",
MultipartFile.fromFileSync(image.path, filename: imageFile)),
]);
return formData;
}
final response = await dio.post(url,
data: formData(),
options: (contentType: 'multipart/form-data', headers: {
"Accept": "application/json",
"Authorization": finalToken,
}),
););
if (response.statusCode == 201) {
return ProfileModel.fromJson(json.jsonDecode(response.data));
} else {
throw Exception("Error");
}
}
}
尝试一下。如果我不得不猜测,我认为这与您的图像路径有关。