【问题标题】:flutter image upload doesnt work on real device altough It works on emulator颤振图像上传在真实设备上不起作用,虽然它适用于模拟器
【发布时间】:2021-05-10 04:57:07
【问题描述】:

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

  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-> 查询)有效,但没有上传...

【问题讨论】:

    标签: php image api flutter upload


    【解决方案1】:

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

    【讨论】:

    • 谢谢老兄,我同时处理后端和前端。我进入 PHP.ini 并将最大上传大小从 2 MB 设置为 40MB :D 它有效
    【解决方案2】:

    添加一个 alse 条件,即

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2016-02-23
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      相关资源
      最近更新 更多