简体   繁体   English

如何将类型文件的图像(从颤振移动应用程序)发送到 php 数据库

[英]How to send Image of type file(From flutter mobile application ) to php database

This is how I picked a file from device这就是我从设备中选择文件的方式

  onPressed: () async {
                                    FilePickerResult? result =
                                        await FilePicker.platform.pickFiles(
                                      type: FileType.custom,
                                      allowedExtensions: [
                                        'jpg',
                                        'pdf',
                                        'doc'
                                      ],
                                    );
                                    List<File> files = result!.paths
                                        .map((path) => File(path!))
                                        .toList();
                                    context
                                        .read<Dropper>()
                                        .fileCheck(result: result);

                                    myfile = files[0];
                                  },

Then I Converted to Uint8List :然后我转换为 Uint8List :

  Uint8List imgbytes = await myFile.readAsBytes();

Now I am sending that file to Php database现在我将该文件发送到 PHP 数据库

 final url = "http://10.0.2.2:8000/api/addSurvey";
  final uri = Uri.parse(url);

  final response = await api.post(uri, headers: {
    'Authorization': token,
  }, body: {
    "bill_image": imgbytes

} }

It throws error msg like this : type 'Uint8List' is not a subtype of type 'String' in type cast它会抛出这样的错误消息: type 'Uint8List' is not a subtype of type 'String' in type cast

  var url = "http://10.0.2.2:8000/api/addSurvey";
                        Map<String, String> headers = {"Content-Type": "application/json",
 'Authorization': token,};
                        var request = http.MultipartRequest("POST", Uri.parse(url));
                        if(_image != null){
                          var pic = await http.MultipartFile.fromBytes('bill_image', imgbytes , filename: 'photo.jpg');
                          request.files.add(pic);
                        }
    request.send().then((result) async {
                          http.Response.fromStream(result).then((response) async {
                            if (response.statusCode == 200)
                            {
                              
                            }
                            else{
                              
                            }

                            return response.body;

                          });
                        }).catchError((err) => print('merror : '+err.toString())).whenComplete(()
                        {

                        });

Try it using multipart request.尝试使用多部分请求。

if php is accepting parameter as file type File then you need to send image as multipart request i guess.如果 php 接受参数作为文件类型文件,那么我猜你需要将图像作为多部分请求发送。

    var headers = {
  'Authorization': 'token'
};
var request = http.MultipartRequest('POST', Uri.parse('http://10.0.2.2:8000/api/addSurvey'));
request.files.add(await http.MultipartFile.fromPath('bill_image', 'path to your file'));
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何flutter/dart发送图片数据到apache服务器php文件 - How to flutter/dart send Image data to apache server php file 如何将图像阵列从一个 PHP 文件发送到另一个 PHP? - How to send a image array from one PHP file to another PHP? 如何将移动jquery制作的表单数据发送到php文件以存储在mysql数据库中 - How to send form data that is made by mobile jquery to a php file to store in mysql database 如何使用ajax从html页面发送文件到php,而该文件又存储在mysql数据库中BLOB类型的列中? - How to send a file from html page using ajax to php which in turn stores in mysql database in column of type BLOB? 如何通过PHP从SMS API发送iOS和Android移动应用程序链接? - How to send Mobile Application Link For iOS and Android In SMS API from PHP? 如何使用PHP中的SMS API从MySQL数据库将批量SMS发送到手机号码 - how to send bulk sms to mobile numbers from mysql database using sms api in php 如何使用php将图像文件从数据库输入[file] - How to put an image file to input[file] from database using php 如何在JQuery页面上获取文件类型数据(图像)并发送到PHP页面进行图像上传 - How to fetch file type data (image) on JQuery page and send to PHP page for image upload 从数据库中检索并发送到另一个php文件 - retrieve from database and send to another php file 将图像文件从angular发送到php - Send image file from angular to php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM