在 Flutter 中使用 http.post 和注册表单上传图片?

编程入门 行业动态 更新时间:2024-10-10 23:26:11
本文介绍了在 Flutter 中使用 http.post 和注册表单上传图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我想用一堆其他变量(字符串)将文件(图像)上传到服务器

so i want to upload a File (image) to a server with a bunch of other variable (Strings)

字符串 firstname , lastname ,birthDay, phone , adresse ;文件图片;

String firstname , lastname , birthDay, phone , adresse ; File image;

return http.post( uri, headers: { 'Accept': 'application/json', "Authorization": "Bearer $token", }, body: body, encoding: encoding, ); Future<http.Response> postRegisteration() async { return await api.httpPost('fotApp/master', body: { 'firstname': 'lorem', 'lastname': 'lorem', 'birthDay': 'lorem', 'adresse': 'lorem', 'phone': 'lorem', 'image': 'lorem' }).then((reponse) { var data = jsonDecode(reponse.body); print(data); }); }

推荐答案

试试这样的

在fileList中,你应该添加任何你想上传的文件

In fileList, you should add any file you like to upload

List<MultipartFile> fileList = List(); fileList.add(MultipartFile.fromBytes( 'documents', await filePath.readAsBytes(), filename: fileName));

其他部分参数使用params map

For other part parameters use params map

Map<String, String> params = { "first_name": widget.mUserDetailsInputmodel.firstName, "last_name": widget.mUserDetailsInputmodel.lastName, "email": widget.mUserDetailsInputmodel.emailAddress, };

然后像这样发送请求

Future<String> multipartRequest({var url, var partParams, var files}) async { Map<String, String> headers = { "X-API-KEY": X_API_KEY, "Accept": "application/json", "User-Auth-Token": authToken }; var request = http.MultipartRequest("POST", Uri.parse(url)); request.headers.addAll(headers); if (partParams != null) request.fields.addAll(partParams);// add part params if not null if (files != null) request.files.addAll(files);// add files if not null var response = await request.send(); var responseData = await response.stream.toBytes(); var responseString = String.fromCharCodes(responseData); print("responseBody " + responseString); if (response.statusCode == 200) return responseString; }

更多推荐

在 Flutter 中使用 http.post 和注册表单上传图片?

本文发布于:2023-10-10 09:40:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   上传图片   Flutter   http   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!