Json 在flatter中将文件发送到api
我正在尝试将一个文件从Flatter应用程序发送到api 此api接受如下值 这里Json 在flatter中将文件发送到api,json,api,flutter,dart,postman,Json,Api,Flutter,Dart,Postman,我正在尝试将一个文件从Flatter应用程序发送到api 此api接受如下值 这里profileImage接受类型文件 这是我在颤振中使用的代码 .................... .................... final File _imgfile = widget.imageFile; prin
profileImage
接受类型文件
这是我在颤振中使用的代码
....................
....................
final File _imgfile = widget.imageFile;
print(_fbName + _fbEmail);
final body = {
"name": _fbName,
"gender": gender,
"bday": bday,
"email": _fbEmail,
"phone": _phone,
"profileImage": _imgfile
};
print(body);
RegisterUserService.RegisterUser(jsonEncode(body))
.then((success) {
if (success) {
print('registration successfull');
//passing data to next screens
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SetupStepThree()));
} else {
print('registration error !');
}
});
..................
这就是上面代码调用的服务
class RegisterUserService {
static Future<bool> RegisterUser(body) async {
final response =
await http.post('${URLS.BASE_URL}/user/register', body:body);
var data = response.body;
// print(body);
print(json.decode(data));
// Map<String, dynamic> res_data = jsonDecode(data);
// log(res_data['loginstatus']);
// if (res_data['loginstatus'] == 'olduser') {
// return true;
// } else {
// return false;
// }
return false;
}
}
**
我想知道这段代码有什么问题,以及如何将图像传递到此端点为了在flatter中上载文件,您可以使用MultipartRequest
Uri uri = Uri.parse("${URLS.BASE_URL}/user/register");
var fileStream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length = await imageFile.length();
var request = new http.MultipartRequest("POST", uri);
//add your fields here
request.fields.add("gender","male");
request.fields.add("name","sssss");
var multipartFile = new http.MultipartFile('file', fileStream, length,
filename: <Your file name>);
request.files.add(multipartFile);
var response = await request.send();
//do whatever you want with the response
Uri=Uri.parse(${URL.BASE_URL}/user/register”);
var fileStream=newhttp.ByteStream(DelegatingStream.typed(imageFile.openRead());
var length=wait imageFile.length();
var request=newhttp.MultipartRequest(“POST”,uri);
//在此处添加您的字段
请求。字段。添加(“性别”、“男性”);
请求。字段。添加(“名称”、“SSS”);
var multipartFile=new http.multipartFile('file',fileStream,length,
文件名:);
request.files.add(多部分文件);
var response=wait request.send();
//你想怎么做就怎么做
让我知道这是否解决了您的问题使用Dio()智能实现多部分文件。它给了你什么?
Uri uri = Uri.parse("${URLS.BASE_URL}/user/register");
var fileStream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length = await imageFile.length();
var request = new http.MultipartRequest("POST", uri);
//add your fields here
request.fields.add("gender","male");
request.fields.add("name","sssss");
var multipartFile = new http.MultipartFile('file', fileStream, length,
filename: <Your file name>);
request.files.add(multipartFile);
var response = await request.send();
//do whatever you want with the response