颤振图像上传在真实设备上不起作用,但它适用于模拟器

时间:2021-02-05 20:34:58

标签: php image api flutter upload

这是我用来将图像上传到数据库的函数。它在模拟器上正常工作,但是当我尝试使用我的真实设备来做它时它不起作用。(我允许互联网许可)

  Future uploadImage() async {

  final SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  var userimidd = sharedPreferences.getString('id');
  DateTime now = DateTime.now().toLocal();
  String tarih = "${now.day}_${now.month}_${now.year}:${now.hour + 3}:${now.minute}";

  final uri = Uri.parse("http://MY IP/uploadavatar.php");
  var request = http.MultipartRequest('POST', uri);
  request.fields['userid'] = userimidd;
  String allofthem = '${userimidd}__${now.day}_${now.month}_${now.year}_${now.minute}_${now.second}_${now.microsecond}.jpg';
  request.fields['useravatarextension'] = allofthem;
  var pic = await http.MultipartFile.fromPath("image",_image.path,filename: allofthem);
  request.files.add(pic);
  var response = await request.send();

  if (response.statusCode == 200) {
    print('Image successfully uploaded');
  } else {
    print('Something bad happened');
  }
}

这是我的api源;

$image = $_FILES['image']['name'];
$userid = $_POST['userid'];
$useravatarextension = $_POST['useravatarextension'];




$imagePath = 'uploads/'.$image;
$tmp_name = $_FILES['image']['tmp_name'];

move_uploaded_file($tmp_name,$imagePath);

$db->query("UPDATE users SET user_picture = 'http://MY IP/uploads/$useravatarextension' WHERE userid = '".$userid."'");

第二个代码($db-> 查询)有效但无法上传...

2 个答案:

答案 0 :(得分:0)

添加一个alse条件即

if(response.statuscode == 200)
{
  // print(result)
}
else
{
  print(response.statucode)
}

然后在设备中运行。记下错误状态代码。从中我们可以调试导致相同的问题。

答案 1 :(得分:0)

我遇到了同样的问题,发现问题出在图像尺寸上(因为真实设备的图像尺寸很大)。所以请告诉您的后端团队处理图像尺寸。