flutter - 如何通过Api上传多张图片
问题描述
我正在尝试通过 Api 上传多个图像,但我不明白如何发送列表,但我可以上传单个图像。尝试了很多搜索但没有帮助,我还导入了 multi_image_picker 我可以选择图像,但问题在于上传。
Future<Map<String, dynamic>> _uploadImage(File image) async {
String value = '';
SharedPreferences pref2 = await SharedPreferences.getInstance();
value = pref2.getString("user_role");
final mimeTypeData =
lookupMimeType(image.path, headerBytes: [0xFF, 0xD8]).split('/');
// Intilize the multipart request
final imageUploadRequest = http.MultipartRequest('POST', apiUrl);
// Attach the file in the request
final file = await http.MultipartFile.fromPath('photo', image.path,
contentType: MediaType(mimeTypeData[0], mimeTypeData[1]));
// Explicitly pass the extension of the image with request body
// Since image_picker has some bugs due which it mixes up
// image extension with file name like this filenamejpge
// Which creates some problem at the server side to manage
// or verify the file extension
imageUploadRequest.files.add(file);
imageUploadRequest.fields['mobile'] = _mobileNo.text;
imageUploadRequest.headers.addAll({
'Content-Type': 'application/json',
'Authorization': Constants.authToken,
});
var response = await imageUploadRequest.send();
if (response.statusCode == 200) print('Done!');
final respStr = await response.stream.bytesToString();
return json.decode(respStr);
}
解决方案
推荐阅读
- javascript - 任务:react-native-webview:compileDebugJavaWithJavac FAILED
- android - Android 模拟器无法在 Android Studio 中运行
- javascript - 如何从 Trefle API 查询信息
- sql - SQL查询不同流服务中用户的重叠?
- html - 插入 FAVICON 的替代方法?
- windows - WEVTUtil 从源过滤
- reactjs - 反应路由器并将道具传递给其他组件
- python - 更改布尔值 tkinter 的按钮
- javascript - 检测已发送图像的消息名称
- python - df.plot 只给出一列