Dio 是Flutter 网络请求的Pub包 Dio除了常用的get post 还可以文件下载 上传等操作
关于文件分片上传 或者文件下载

用到的pub 包

  #文件选择file_selector: ^0.8.2file_selector_windows: ^0.8.2file_selector_macos: ^0.0.4file_selector_linux: ^0.0.2file_selector_web: ^0.8.1#网络请求dio: ^4.0.0

文件分片上传

  • 获取本地文件信息[路径 名称 大小 等]
  • 文件分片
  • 上传分片到服务器
  • 结束

获取本地文件信息

 _openLocalSingleFile() async {try {final xTypeGroup =XTypeGroup(label: '资源类型(jpg,png,mp4,gif)', extensions: ['jpg','jpeg','png','gif','mp4',]);final XFile = await openFiles(acceptedTypeGroups: [xTypeGroup]);List<LocalFile> items = [];for (var e in XFile) {items.add(LocalFile(e.path,mineType: e.mimeType,fileType: e.name.contains('mp4') ? 1 : 0,name: e.name,length: 0,));}_mainController.addSelLocalFilesToUpload(items);} catch (e) {}}
}

文件分片&上传分片到服务器

/// count 当前上传进度
/// total 上传总长度
/// type 转码状态
typedef UploadProgressCallBack = Function(int count, int total, {int? type});///文件分片上传///url 文件上传地址///localFilePath 本地文件地址///progressCallBack 上传进度///videoConvertModel 视频信息static Future<bool> uploadSliceFile(String url, String localFilePath,UploadProgressCallBack? progressCallBack,{AddVideoConvertModel? videoConvertModel}) async {try {final String fileUrl = await Global.getFileUrl();final String token = await Global.getUserToken();var xFile = XFile(localFilePath);int startOffset = 0;bool upload = true;var xRead = await xFile.readAsBytes(); //读取字节流List<int> bit = xRead.toList();var length = bit.length;while (upload) { //分片上传List<int> bitU = [];int isLast = 0;if (startOffset == 0) {if (length <= 1000 * 1024) {bitU = bit;isLast = 1;} else {bitU = bit.sublist(startOffset, startOffset + (1000 * 1024));}} else {if ((length - startOffset) <= 1000 * 1024) {isLast = 1;bitU = bit.sublist(startOffset);} else {bitU = bit.sublist(startOffset, startOffset + (1000 * 1024));}}///上传文件分片var fileResponseModel =await _upload(fileUrl, token, url, bitU, startOffset, isLast,(count, t, {int? type}) {if (progressCallBack != null) {progressCallBack(count + startOffset, length, type: type);}});if (fileResponseModel != null) {startOffset = fileResponseModel.startOffset ?? 0;upload = !(fileResponseModel.isSuccess == true && isLast == 1);}}///视频转码=============if (Extension.fileType(url) == 1 && videoConvertModel != null) {var result =await _postAddVideoConvertTask(fileUrl, token, videoConvertModel) ??false;if (result == true) {bool r = true;while (r) {var result = await _getVideoConvertStatus(fileUrl, token, videoConvertModel.videoId);if (result == 1) {//转码成功r = false;print('转码成功==============');if (progressCallBack != null) {progressCallBack(0, 0, type: 1);}} else {print('${result == 3 ? '转码中' : ''}');if (progressCallBack != null) {progressCallBack(0, 0, type: 3);}}await Future.delayed(Duration(seconds: 5));}return true;} else {return false;}}return true;} catch (e) {print(e);}return false;}static Future<FileResponseModel?> _upload(String fileUrl,String token,String url,List<int> bit,int offset,int isLast,UploadProgressCallBack? progressCallBack) async {var fromFile = await MultipartFile.fromBytes(bit, filename: '');final Dio dio = Dio(BaseOptions(baseUrl: fileUrl + "/",sendTimeout: timeOut,method: "POST",connectTimeout: timeOut,headers: {'Auth': token,'Charset': 'UtF-8','Connection': 'Keep-Alive','Content-Type': 'multipart/form-data;boundary=----------'}));var formData = FormData.fromMap({'file': fromFile});final r = await dio.post<String>('api/FileUpload/UploadCarveFile',queryParameters: {'relativeServerName': url,'offset': offset,'isLast': isLast,'isRetainOrigianl':Extension.fileType(url) == 0 || Extension.fileType(url) == 1? 1: 0},data: formData, onSendProgress: (int count, int total) {var callBack = progressCallBack ?? (int count, int total, {int? type}) {};callBack(count, total, type: 0);});var httpResultModelTwo =HttpResultModelTwo.fromJson(json.decode(r.data.toString()));if (httpResultModelTwo.responseStatus == 1) {return FileResponseModel.fromJson(httpResultModelTwo.data!);}return null;}

文件下载

  ///下载文件到本地///urlPath 文件Url///savePath 本地保存位置///downloadProgressCallBack 下载文件回调static Future<Response> downloadFile(String urlPath, String savePath,{DownloadProgressCallBack? downloadProgressCallBack}) async {Dio dio = Dio();return await dio.download(urlPath, savePath,onReceiveProgress: downloadProgressCallBack);}/// count 当前下载进度
/// total 下载总长度
typedef DownloadProgressCallBack = Function(int count, int total);

Flutter dio 文件上传下载相关推荐

  1. 教你如何实现c#文件上传下载功能

    简单介绍一下c#文件上传下载功能实现. NuGet 安装SqlSugar Model文件下新建 DbContext 类 public class DbContext {public DbContext ...

  2. [C# 网络编程系列]专题十一:实现一个基于FTP协议的程序——文件上传下载器...

    引言: 在这个专题将为大家揭开下FTP这个协议的面纱,其实学习知识和生活中的例子都是很相通的,就拿这个专题来说,要了解FTP协议然后根据FTP协议实现一个文件下载器,就和和追MM是差不多的过程的,相信 ...

  3. SpringMVC整合fastdfs-client-java实现web文件上传下载

    为什么80%的码农都做不了架构师?>>>    版权声明:本文为博主原创文章,转载请标明出处(http://blog.csdn.net/wlwlwlwl015)Thanks. 目录( ...

  4. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  5. 文件上传 java web_JavaWeb 文件上传下载

    1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图片或网盘等功能都是利用文件上传下 ...

  6. ASP.NET中文件上传下载方法集合

    asp.net 2008-08-23 21:10:35 阅读0 评论0   字号:大中小 订阅 ASP.NET中文件上传下载方法集合 文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,这里给 ...

  7. salesforce 零基础学习(四十二)简单文件上传下载

    项目中,常常需要用到文件的上传和下载,上传和下载功能实际上是对Document对象进行insert和查询操作.本篇演示简单的文件上传和下载,理论上文件上传后应该将ID作为操作表的字段存储,这里只演示文 ...

  8. ASP.NET中常用的文件上传下载方法

    ASP.NET中常用的文件上传下载方法 文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,这里给出几种常见的方法,本文主要内容包括: 1.如何解决文件上传大小的限制 2.以文件形式保存到服务 ...

  9. java实现excel文件上传_java相关:SpringMVC下实现Excel文件上传下载

    java相关:SpringMVC下实现Excel文件上传下载 发布于 2020-6-21| 复制链接 摘记: 在实际应用中,经常会遇到上传Excel或者下载Excel的情况,比如导入数据.下载统计数据 ...

最新文章

  1. List,Map,实体类,字符串相互转换
  2. java se 1335,1335.逼退法王
  3. 独家对话谢宝友:做一款类似于 Linux 的国产操作系统 | 人物志
  4. python入门24 json模块
  5. 教你如何清除计算机病毒
  6. Kolmogorov复杂性简介(转)
  7. Windows系统中禁止某应用程序联网操作方法
  8. PySpark机器学习 ML
  9. 最好的时光在路上,最美的风景在远方
  10. python制作白底界面_python 多张图片黑底白字转白底黑字
  11. ng : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\ng.ps1,因为在此系统上禁止运行脚本
  12. 生活中的定律——墨菲定律
  13. ucenter用户中心头像修改,不使用自带方法,不使用flash 转
  14. 黑马pink JavaScript笔记(7)-流程控制-循环
  15. 删除windows搜索框中的搜索记录
  16. 前端小练习:纯css菜单栏
  17. 苏宁易购商品详情 API
  18. 英文面试四——where do you see yourself in five years
  19. linux centos7 增加ipv6配置
  20. GEE 形态学运算Morphological Operations

热门文章

  1. linux常用指令介绍_软件包管理_VIM编辑器的使用_用户和组账户管理_文件权限管理
  2. “网络三结义”--mpls 进阶实验详细配置
  3. 计算机插u盘抖动,电脑插入U盘后发现读取速度不稳定如何解决
  4. 等比数列二分求和(首项为0次项与1次项的方法)
  5. QML一个漂亮的仪表盘
  6. STP——MSTP理论讲解
  7. 女巫店2011年12星座年度运势分析
  8. 微信小程序(第十九章)- 用户评价页面实现
  9. 线性表-链式存储结构
  10. java 二维数组存储方式_JAVA-初步认识-第六章-二维数组-定义方式内存图解