如何上传flutter中file_picker选中的文件

7dl7o3gd  于 2022-12-24  发布在  Flutter
关注(0)|答案(2)|浏览(287)

我需要发送一个附件到一个电子邮件中挑选的文件_picker从flutter。如何发送文件。

Padding(
          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
          child: RaisedButton(
            elevation: 0,
            onPressed: () async{
              final result = await FilePicker.platform.pickFiles();
              if(result == null) return;
              final PlatformFile file = result.files.first;
              setState(() {
                qatarId = file.name;
              });
              openFile(file);
            },
            color: Colors.white,
            shape: RoundedRectangleBorder(
              side: BorderSide(
                color: Colors.green,
                width: 1),
              borderRadius: BorderRadius.all(Radius.circular(0),
              ),
            ),
            child: Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    'Qatar ID',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.normal,
                      color: Colors.green,
                    ),
                  ),
                  Icon(
                    Icons.upload_sharp,
                    color: Colors.green,
                  )
                ],
              ),
            ),
          ),
        ),

如何定义上传图片。图片是从file_picker中获取的。

ukdjmx9f

ukdjmx9f1#

使用dio库功能强大的Http客户端,支持拦截器,全局配置,表单数据,请求取消,文件下载,超时等。

FilePickerResult? result = await FilePicker.platform.pickFiles(
                      type: FileType.custom,
                      allowedExtensions: ['jpg', 'pdf', 'doc', 'png', 'mp4', 'mkv'],
                    );
                    if (result != null) {
                      PlatformFile file = result.files.first;
                
                      print(file.name);
                      print(file.bytes);
                      print(file.size);
                      print(file.extension);
                      print(file.path);
                 var multipartFile = await MultipartFile.fromFile(file.path,
                       );
                    FormData formData = FormData.fromMap({
                
                
                
                      "MediaFile": multipartFile,  //define your json data here
                    });
            
            
             functionToUploadToServer(formData); //call upload function passing multiform data
                }
    }
            
            
            
            
                functionToUploadToServer(multiformData){  
                    
                      Dio dio = Dio();
                
                      final response = await dio.post(
                        url,
                        data: multiformData,
                        options: Options(
                            contentType: 'multipart/form-data',
                            headers: {},
                            followRedirects: false,
                            validateStatus: (status) {
                              return status! <= 500;
                            }),
                      );
                      print(response.statusCode);
                     
                }
mwngjboj

mwngjboj2#

This _upload()函数需要一个图像列表,带有标题的API url,然后将图像列表上传到服务器。

void _upload(List<XFile> files) async {
 for (var file in files) {
  String fname = filename;
  FormData data = FormData.fromMap({
   "file": await MultipartFile.fromFile(
     file.path,
     filename: fname,
    ),
  });

  Dio dio = new Dio();
 
  print('Image File Name: $fname');
  var response = await dio.put(url,
    data: data,
    options: Options(
      headers: {},
    ),
  );

  print(response.statusCode);
  print(response.data);
 }
}

相关问题