flutter 将图片保存在Android模拟器的资产文件夹中并将其发布到API

qhhrdooz  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(82)

我是Flutter的初学者,我在android模拟器图库中有一张图片,我正试图通过API更新我的用户配置文件图片。
我的API函数:这是我更新它的方式(顺便说一下,电话字段得到更新,而图像没有)我收到一个状态代码200

Future<Provider> updateProviderInfo(Provider person) async {

 person.idUser = await FlutterSession().get("id");
final response = await http.put(
  Uri.parse(ApiConstants.BASE_URL + ApiConstants.UPDATE_PROVIDER),
  headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
  },
  body: jsonEncode(<String, String>{
    'providerId': person.idUser.toString(),
    'providerProfilePicture': person.providerProfilePicture,
    'phone' :"92211130030"
  }),
);
if (response.statusCode == 200) {
  // If the server did return a 200 OK response,
  // then parse the JSON.
  Fluttertoast.showToast(
      msg: "Updated with success",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.blue,
      textColor: Colors.white,
      fontSize: 16.0);
  return Provider.fromJson(jsonDecode(response.body));
} else {
  // If the server did not return a 200 OK response,
  // then throw an exception.
  throw Exception('Failed to update User Type :Service Provider.');
}}

如何加载图像:
这是我从模拟器的画廊或相机获取图像的方式:

Future<File> _loadImage(ImageSource imageSource) async {

PickedFile file = await _imagePicker.getImage(source: imageSource,imageQuality: 50, // <- Reduce Image quality
    maxHeight: 250,  // <- reduce the image size
    maxWidth: 250);
File mFile;

if (null != file) {
  Directory directory = await getTemporaryDirectory();
  //mFile = await _saveImageToDisk(file.path, directory);
  Map map = Map();
  map['path'] = file.path;
  String myLastFilePath = file.path.substring(file.path.indexOf("picker"));
  print ("my try : "+myLastFilePath);

  provider.providerProfilePicture = myLastFilePath;
  print("my path :  "+provider.providerProfilePicture);
  map['directory'] = directory;
  mFile = await compute(_saveImageToDisk, map);
}
return mFile; }

This is what my view looks like
我收到一个200状态代码,电话号码正在更新,但DB中的图像字段为空
my Route API test in postman
我想我应该使用multiPartFormData,但是我不知道如何使用它,而且我不确定是否需要任何指导谢谢

xwmevbvl

xwmevbvl1#

下面是使用多部件请求的代码:

static Future<bool> uploadFile(File file) async {
  // open a byteStream
  var stream = http.ByteStream(file.openRead());
  stream.cast();

  // get file length
  var length = await file.length();

  Uri uri = Uri.parse(ApiConstants.BASE_URL + ApiConstants.UPDATE_PROVIDER);

  // create multipart request
  var request = http.MultipartRequest("POST", uri);

  // multipart that takes file
  var multipartFile = http.MultipartFile(
    'file',
    stream,
    length,
    filename: basename(file.path),
  );

  // add file to multipart
  request.files.add(multipartFile);

  // send
  var response = await request.send();
  // print(response.statusCode);
  if (response.statusCode == 200) {
    ...
  } else {
    ...
  }
}

根据您的需要更新此代码并查看它是否工作。您可能需要稍微修改服务器端代码以使其正常工作。

相关问题