在本文中主要分享Flutter中的Dio文件实现上传并实现进度监听https://zhuanlan.zhihu.com/p/145447902 - ///手机中的图片
- String localImagePath ="/storage/emulated/0/Download/17306285.jpg";
- ///上传的服务器地址
- String netUploadUrl = "http://192.168.0.102:8080/fileupload";
- ///dio 实现文件上传
- void fileUplod() async{
- ///创建Dio
- Dio dio = new Dio();
- Map<String ,dynamic> map = Map();
- map["auth"]="12345";
- map["file"] = await MultipartFile.fromFile(localImagePath,filename: "xxx23.png");
- ///通过FormData
- FormData formData = FormData.fromMap(map);
- ///发送post
- Response response = await dio.post(netUploadUrl, data: formData,
- ///这里是发送请求回调函数
- ///[progress] 当前的进度
- ///[total] 总进度
- onSendProgress: (int progress, int total) {
- print("当前进度是 $progress 总进度是 $total");
- },);
- ///服务器响应结果
- var data = response.data;
- }
复制代码
|
|