如何将图像作为参数传递给 post 请求 Flutter 中的键?
How to pass an image as parameter to a key in a post request Flutter?
请求正文是 form-data
。请求正文有一个键 image
需要一个文件(这里是图像文件)。这是我到目前为止所做的:
final response = await http.MultipartRequest(
'POST', Uri.parse(Constant.baseUrl + 'api/patients'))
..headers.addAll({"Authorization": "Bearer $token"})
..fields.addAll({
"first_name": firstName,
"last_name": lastName,
"email": email,
"date_of_birth": dob,
"address": address,
"gender": gender,
"blood_group": bg,
"primary_phone": primaryPhone,
"alt_phone": altPhone
})
..files.add(await http.MultipartFile.fromPath(
'image',
image.path,
));
我在此处附上邮递员请求正文,以便更好地理解我的问题。最后一把钥匙是我遇到的麻烦。
我想我只是在上传没有密钥的图像文件。这就是为什么我的请求有效但图像仍未发布的原因。这里的图像文件是一个 XFile(使用 FLutter 中的 image_picker 库)。
我还想使用 http
库来实现。我知道可以通过 dio
。但只是为了这个要求,我不想导入另一个库。
uploadTest(FilePickerResult result, String url, docname, formId,
{lastService: false}) async {
print(result);
if (result != null) {
String filename = docname;
final file = result.files.first;
// if (lastService == true) {
filename = docname + '.' + file.name.split(".")[1];
print("filename is $filename");
// }
var uri = Uri.parse(url);
var stream = http.ByteStream(file.readStream);
var request = http.MultipartRequest('POST', uri);
final mimeType = lookupMimeType(file.name ?? '');
var val1 = {
"customerId": variableMap['customerId'],
"FormId": formId,
"docName": filename
};
final val2 = json.encode(val1);
request.fields['val'] = val2;
var multipartFile = http.MultipartFile(
'myFile',
stream,
file.size,
filename: filename,
contentType: mimeType == null ? null : MediaType.parse(mimeType),
);
request.files.add(multipartFile);
final httpClient = http.Client();
final response = await httpClient.send(request);
if (response.statusCode != 200) {
throw Exception('HTTP ${response.statusCode}');
}
final body = await response.stream.transform(utf8.decoder).join();
print(body);
}
}
// 我正在发布我使用的方法.. 没有定制为您的用例。它从 filePicker
执行图像上传
http.MultipartRequest request = http.MultipartRequest(
"POST",
Uri.parse(baseUrl + "api/profiles/v1/update/"),
)
..fields['first_name'] = firstName
..fields['last_name'] = lastName
..fields['phone'] = phone
..fields['full_name'] = fullName
..fields['country'] = 'Uzbekistan'
..fields['city'] = 'Tashkent'
..fields['date_birth'] = dateBirth!;
request.files.add(await http.MultipartFile.fromPath(
'image',
image.path,
filename: image.path.split('/').last,
)
);
var response = await request.send();
var responsed = await http.Response.fromStream(response);
print(responsed.body);
请求正文是 form-data
。请求正文有一个键 image
需要一个文件(这里是图像文件)。这是我到目前为止所做的:
final response = await http.MultipartRequest(
'POST', Uri.parse(Constant.baseUrl + 'api/patients'))
..headers.addAll({"Authorization": "Bearer $token"})
..fields.addAll({
"first_name": firstName,
"last_name": lastName,
"email": email,
"date_of_birth": dob,
"address": address,
"gender": gender,
"blood_group": bg,
"primary_phone": primaryPhone,
"alt_phone": altPhone
})
..files.add(await http.MultipartFile.fromPath(
'image',
image.path,
));
我在此处附上邮递员请求正文,以便更好地理解我的问题。最后一把钥匙是我遇到的麻烦。
我想我只是在上传没有密钥的图像文件。这就是为什么我的请求有效但图像仍未发布的原因。这里的图像文件是一个 XFile(使用 FLutter 中的 image_picker 库)。
我还想使用 http
库来实现。我知道可以通过 dio
。但只是为了这个要求,我不想导入另一个库。
uploadTest(FilePickerResult result, String url, docname, formId,
{lastService: false}) async {
print(result);
if (result != null) {
String filename = docname;
final file = result.files.first;
// if (lastService == true) {
filename = docname + '.' + file.name.split(".")[1];
print("filename is $filename");
// }
var uri = Uri.parse(url);
var stream = http.ByteStream(file.readStream);
var request = http.MultipartRequest('POST', uri);
final mimeType = lookupMimeType(file.name ?? '');
var val1 = {
"customerId": variableMap['customerId'],
"FormId": formId,
"docName": filename
};
final val2 = json.encode(val1);
request.fields['val'] = val2;
var multipartFile = http.MultipartFile(
'myFile',
stream,
file.size,
filename: filename,
contentType: mimeType == null ? null : MediaType.parse(mimeType),
);
request.files.add(multipartFile);
final httpClient = http.Client();
final response = await httpClient.send(request);
if (response.statusCode != 200) {
throw Exception('HTTP ${response.statusCode}');
}
final body = await response.stream.transform(utf8.decoder).join();
print(body);
}
}
// 我正在发布我使用的方法.. 没有定制为您的用例。它从 filePicker
执行图像上传http.MultipartRequest request = http.MultipartRequest(
"POST",
Uri.parse(baseUrl + "api/profiles/v1/update/"),
)
..fields['first_name'] = firstName
..fields['last_name'] = lastName
..fields['phone'] = phone
..fields['full_name'] = fullName
..fields['country'] = 'Uzbekistan'
..fields['city'] = 'Tashkent'
..fields['date_birth'] = dateBirth!;
request.files.add(await http.MultipartFile.fromPath(
'image',
image.path,
filename: image.path.split('/').last,
)
);
var response = await request.send();
var responsed = await http.Response.fromStream(response);
print(responsed.body);