如果图像可以是jpeg或png,我应该在内容类型中指定什么媒体类型?
var postUri = Uri.parse('$serverUrl/store/signup');
var request = http.MultipartRequest('POST', postUri)
..fields['username'] = 'Username'
..files.add(await http.MultipartFile.fromPath(
'profilePic',
profilePic!.path,
contentType: MediaType('image', 'jpeg'),
));
var response = await request.send();
您应该指定正确的类型。image/png
为PNG,image/jpeg
为JPEG。媒体类型的全部意义在于告诉对方你正在发送的数据的实际类型。
当接收数据时,你可以指定多种不同的媒体类型,你将接受,或指定通配符(例如image/*
)。但是当你发送数据时,它不能同时是JPEG和PNG格式。因此,您必须声明实际的类型。