【发布时间】:2021-04-01 22:45:28
【问题描述】:
我想通过多部分文件请求上传图片。使用此代码当我传递两个图像文件时,它工作正常。但是当我想传递一个图像文件而另一个为空时,它就不起作用了。 问题出在哪里?我该如何解决这个问题?
这是我的代码 -
Future<Map<String, dynamic>> updateprofile(
UpdateProfileInfo updateProfileInfo,
File imageFile,
File signatureFile) async {
String url = "$baseAPIUrl/update-profile-info";
String _token = await SavedData().loadToken();
String authorization = "Bearer $_token";
final headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
"Authorization": authorization
};
var request = http.MultipartRequest("POST", Uri.parse(url));
request.headers.addAll(headers);
request.fields.addAll(updateProfileInfo.toJson());
request.files
.add(await http.MultipartFile.fromPath('image', imageFile.path));
request.files.add(
await http.MultipartFile.fromPath('signature', signatureFile.path));
print(" Update Profile Json ${updateProfileInfo.toJson()}");
print("Request Fields ${request.fields}");
http.StreamedResponse response = await request.send();
String respStr = await response.stream.bytesToString();
dynamic respJson;
try {
respJson = jsonDecode(respStr);
} on FormatException catch (e) {
print(e.toString());
}
print('API ${response.statusCode}\n $respJson');
bool isSuccess = response.statusCode == 200;
var data = json.decode(respStr);
return {
'isSuccess': isSuccess,
"message": isSuccess ? data["success"]["message"] : null,
"name": isSuccess ? data["success"]["name"] : null,
"classgroup": isSuccess ? data["success"]["classgroup"] : null,
"image": isSuccess ? data["success"]["image"] : null,
"error": isSuccess ? null : data['error']['message'],
};
}
这里是邮递员截图
1.
2。 POSTMAN 为 Dart 生成的代码 - http
【问题讨论】:
-
你怎么知道 " ...它不起作用。"?
-
表示您想在一个参数中为单个请求传递多个图像对吗?
-
@ShubhamNarkhede 抱歉,没有一个参数。我传递了两个参数 File imageFile 和 File signatureFile
-
@pskink 看到我的输出,好像是这样,我的 API 没有命中
标签: api flutter http multipart