无法使用 DIO 将图像上传到服务器
Image can't be uploaded to the server in flutter using DIO
所以我尝试使用名为 ASP.NET core 的后端框架来更新我的个人资料用户。当我尝试在邮递员中使用 API 时它工作正常,它 100% 工作,它从电子邮件、图像等更改所有用户配置文件。但问题是当我尝试使用 API在flutter中,是不行的。当我尝试调试它时,我发现模型中的一个变量无法从图像中捕获多部分文件,这会出错。
我尝试解决这个问题的方法(但仍然不起作用):
- 我更改了分辨率
- 我更改了图片的宽高
邮递员正在工作
https://i.stack.imgur.com/lj1af.png
UserProfileServices.dart
Future<Response> updateProfile(
File profilePic,
String email,
String password,
String fullName,
String phoneNumber,
DateTime dateOfBirth,
String gender,
String address,
String city,
String province,
String postalcode
)async{
final _path="/user/update-user";
try{
FormData formData= FormData.fromMap(({
"Image": await MultipartFile.fromFile(profilePic.path,
filename: profilePic.path.split("/").last),
"Email":email,
"Password":password,
"FullName":fullName,
"DateOfBirth":dateOfBirth,
"Gender":gender,
"Address":address,
"City":city,
"province":province,
"postalcode":postalcode,
}));
Response response = await httpClient.put(_path,data:formData);
return response;
}
catch(e)
{
print(e);
return e.response;
}
}
正在调试,profilepic变量成功获取图片路径,但图片抓不到
https://i.stack.imgur.com/a5AU7.png
安装并导入 mime、dio 和 http(以防万一)
import 'package:dio/dio.dart';
import 'package:mime/mime.dart';
import 'package:http_parser/http_parser.dart';
代码
Future<Response> updateProfile(File profilePic,String email,String password,String fullName,String phoneNumber,DateTime dateOfBirth,String gender,String address,String city,String province,String postalcode)async{
final _path="/user/update-user";
try{
Dio dio = new Dio();
final mimeTypeData =lookupMimeType(profilePic.path, headerBytes: [0xFF, 0xD8]).split('/');
FormData formData= FormData.fromMap(({
"Image": await MultipartFile.fromFile(profilePic.path,contentType: MediaType(mimeTypeData[0], mimeTypeData[1]),
"Email":email,
"Password":password,
"FullName":fullName,
"DateOfBirth":dateOfBirth,
"Gender":gender,
"Address":address,
"City":city,
"province":province,
"postalcode":postalcode,
}));
var response = await dio.post("enter your full url for api here",data: formData);
return response;
}
catch(e)
{
print(e);
return e.response;
}
}
注意: 检查 formData 是否具有所有值
如果你不想使用Dio你可以使用没有dio的Multipart方法,你可以参考下面的link
伙计们,我的个人资料服务中缺少一个键,即“phonenumber”键,这使它出错
所以我尝试使用名为 ASP.NET core 的后端框架来更新我的个人资料用户。当我尝试在邮递员中使用 API 时它工作正常,它 100% 工作,它从电子邮件、图像等更改所有用户配置文件。但问题是当我尝试使用 API在flutter中,是不行的。当我尝试调试它时,我发现模型中的一个变量无法从图像中捕获多部分文件,这会出错。
我尝试解决这个问题的方法(但仍然不起作用):
- 我更改了分辨率
- 我更改了图片的宽高
邮递员正在工作
https://i.stack.imgur.com/lj1af.png
UserProfileServices.dart
Future<Response> updateProfile(
File profilePic,
String email,
String password,
String fullName,
String phoneNumber,
DateTime dateOfBirth,
String gender,
String address,
String city,
String province,
String postalcode
)async{
final _path="/user/update-user";
try{
FormData formData= FormData.fromMap(({
"Image": await MultipartFile.fromFile(profilePic.path,
filename: profilePic.path.split("/").last),
"Email":email,
"Password":password,
"FullName":fullName,
"DateOfBirth":dateOfBirth,
"Gender":gender,
"Address":address,
"City":city,
"province":province,
"postalcode":postalcode,
}));
Response response = await httpClient.put(_path,data:formData);
return response;
}
catch(e)
{
print(e);
return e.response;
}
}
正在调试,profilepic变量成功获取图片路径,但图片抓不到
https://i.stack.imgur.com/a5AU7.png
安装并导入 mime、dio 和 http(以防万一)
import 'package:dio/dio.dart';
import 'package:mime/mime.dart';
import 'package:http_parser/http_parser.dart';
代码
Future<Response> updateProfile(File profilePic,String email,String password,String fullName,String phoneNumber,DateTime dateOfBirth,String gender,String address,String city,String province,String postalcode)async{
final _path="/user/update-user";
try{
Dio dio = new Dio();
final mimeTypeData =lookupMimeType(profilePic.path, headerBytes: [0xFF, 0xD8]).split('/');
FormData formData= FormData.fromMap(({
"Image": await MultipartFile.fromFile(profilePic.path,contentType: MediaType(mimeTypeData[0], mimeTypeData[1]),
"Email":email,
"Password":password,
"FullName":fullName,
"DateOfBirth":dateOfBirth,
"Gender":gender,
"Address":address,
"City":city,
"province":province,
"postalcode":postalcode,
}));
var response = await dio.post("enter your full url for api here",data: formData);
return response;
}
catch(e)
{
print(e);
return e.response;
}
}
注意: 检查 formData 是否具有所有值
如果你不想使用Dio你可以使用没有dio的Multipart方法,你可以参考下面的link
伙计们,我的个人资料服务中缺少一个键,即“phonenumber”键,这使它出错