Flutter下载文件到本地

本示例中使用第三方库 flutter_downloader 使用Getx状态管理

在pubspec.yaml 引入插件
dependencies: 
flutter_downloader: ^1.11.6
在mian.dart中初始化插件

await FlutterDownloader.initialize(
  debug:
  true, // optional: set to false to disable printing logs to console (default: true)
  ignoreSsl:
  true // option: set to false to disable working with http links (default: false)
);
实际使用 文件详情controller
///声明接收端口
final ReceivePort _port = ReceivePort();

///下载状态
bool isDown = false;

///本地文件路径
late String _saveLocalPath;

///文件下载ID
dynamic taskId = 0;

///下载文档
Future<void> downloaderPdf() async {
    if (filePath.isEmpty) {
      print('文件地址为空');
      return;
    }
    if (isDown) {
      print('正在下载,请勿重复点击');
      return;
    }
    ///loading
    isDown = true;
    print('下载中...');
    taskId = await FlutterDownloader.enqueue(
      url: filePath, ///文件地址
      savedDir: _saveLocalPath,
      fileName: '测试文档',
      showNotification:
          false, ///在状态栏显示下载进度(仅限Android)
      openFileFromNotification:
          false, ///点击通知打开下载的文件 (仅限Android)
    );
    ///合成本地路径 可以用来缓存 在下载记录中直接打开
    localFilePath = '$_saveLocalPath/${fileDetails.filename}';
    update();
}


///下载监听
void _bindBackgroundIsolate() {
    final isSuccess = IsolateNameServer.registerPortWithName(
      _port.sendPort,
      'downloader_send_port',
    );
    if (!isSuccess) {
      _unbindBackgroundIsolate();
      _bindBackgroundIsolate();
      return;
    }
    _port.listen((dynamic data) async {
      ///重新下载状态
      isDown = false;
      final taskId = (data as List<dynamic>)[0] as String;
      final status = data[1];
      final progress = data[2] as int;
      if (status == 3) {
        print('下载完成');
        ///打开文件
        ///删除下载任务
        await FlutterDownloader.remove(
          taskId: taskId,
          shouldDeleteContent: true,
        );
      }
      ///其他status 自行处理
    });
  }

///释放监听
void _unbindBackgroundIsolate() {
  IsolateNameServer.removePortNameMapping('downloader_send_port');
}

///注册监听事件
('vm:entry-point')
static void downloadCallback(
  String id,
  int status,
  int progress,
) {
  IsolateNameServer.lookupPortByName('downloader_send_port')
      ?.send([id, status, progress]);
}


void onInit() {
  super.onInit();
  WidgetsBinding.instance.addPostFrameCallback((_) async {
    _bindBackgroundIsolate();
    FlutterDownloader.registerCallback(downloadCallback);
    ///获取文件存储路径
    _saveLocalPath = await FileDirPath().prepareSaveDir();
    update();
  });
}

///页面关闭销毁监听

onClose() {
  _unbindBackgroundIsolate();
  super.onClose();
}

FileDirPath() 工具类 获取本地存储路径

导入对应插件

  # 文件
  path_provider: ^2.1.0
    
  # 权限请求
  permission_handler: ^11.0.0

  # 设备信息
  device_info_plus: ^9.1.1
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class FileDirPath {
  late bool _permissionReady;

  Future<void> retryRequestPermission() async {
    final hasGranted = await _checkPermission();

    if (hasGranted) {
      await prepareSaveDir();
    }
    _permissionReady = hasGranted;
  }

  ///获取文件保存路径
  Future<String> prepareSaveDir() async {
    String localPath = (await _getSavedDir())!;
    final savedDir = Directory(localPath);
    if (!savedDir.existsSync()) {
      await savedDir.create();
    }
    return localPath;
  }

  Future<String?> _getSavedDir() async {
    String? externalStorageDirPath;
    final Directory? downloadsDir;
    if (Platform.isAndroid) {
      try {
        downloadsDir = await getDownloadsDirectory();
        externalStorageDirPath = downloadsDir!.path;
        print('externalStorageDirPath: $externalStorageDirPath');
      } catch (err, st) {
        print('failed to get downloads path: $err, $st');
        final directory = await getExternalStorageDirectory();
        externalStorageDirPath = directory?.path;
      }
    } else if (Platform.isIOS) {
      // var dir = (await _dirsOnIOS)[0]; // temporary
      // var dir = (await _dirsOnIOS)[1]; // applicationSupport
      // var dir = (await _dirsOnIOS)[2]; // library
      var dir = (await _dirsOnIOS)[3]; // applicationDocuments
      //var dir = (await _dirsOnIOS)[4]; // downloads
      dir ??= await getApplicationDocumentsDirectory();
      externalStorageDirPath = dir.absolute.path;
    }
    return externalStorageDirPath;
  }

  Future<List<Directory?>> get _dirsOnIOS async {
    final temporary = await getTemporaryDirectory();
    final applicationSupport = await getApplicationSupportDirectory();
    final library = await getLibraryDirectory();
    final applicationDocuments = await getApplicationDocumentsDirectory();
    final downloads = await getDownloadsDirectory();
    final dirs = [
      temporary,
      applicationSupport,
      library,
      applicationDocuments,
      downloads
    ];
    return dirs;
  }

  Future<bool> _checkPermission() async {
    if (Platform.isIOS) {
      return true;
    }
    if (Platform.isAndroid) {
      final info = await DeviceInfoPlugin().androidInfo;
      if (info.version.sdkInt > 28) {
        return true;
      }

      final status = await Permission.storage.status;
      if (status == PermissionStatus.granted) {
        return true;
      }

      final result = await Permission.storage.request();
      return result == PermissionStatus.granted;
    }

    throw StateError('unknown platform');
  }
}

总结
  1. flutter_downloader 集成配置参考插件官方文档
  2. **Android 存储权限变更 : **Android13以下需要声明存储权限,以上则不需要默认已授权
  3. 处理下载回调:flutter_downloader 插件提供了下载的回调函数,你可以利用这些回调来处理下载的状态变化,例如进度更新或下载完成。确保在你的应用中处理这些回调以提供更好的用户体验。
  4. 错误处理: 考虑在下载过程中处理可能的错误情况,例如网络不稳定、存储空间不足等。提供清晰的错误信息以便用户能够了解问题并可能采取相应的措施。
  5. 释放资源: 当你不再需要下载任务时,确保正确地释放资源,以避免潜在的内存泄漏
  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Flutter中上传本地文件,您需要使用Flutter中提供的dart:io库。首先,使用FilePicker库选择要上传的文件。然后,使用http包中的MultipartRequest,将文件作为多部分请求附加到请求体中。最后,使用http包中的HttpClient将请求发送到服务器。以下是一个示例代码: ```dart import 'dart:io'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'package:file_picker/file_picker.dart'; class FileUpload extends StatefulWidget { @override _FileUploadState createState() => _FileUploadState(); } class _FileUploadState extends State<FileUpload> { File file; void _openFileExplorer() async { file = await FilePicker.getFile(type: FileType.any); setState(() {}); } Future _uploadFile() async { var request = http.MultipartRequest( 'POST', Uri.parse('your-upload-api-url-here'), ); request.files.add(await http.MultipartFile.fromPath('file', file.path)); request.headers.addAll({'Authorization': 'Bearer your_access_token_here'}); var response = await request.send(); if (response.statusCode == 200) { // handle success } else { // handle error } } @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ FlatButton( onPressed: _openFileExplorer, child: Text('Select File'), ), file != null ? Text(file.path) : Container(), RaisedButton( onPressed: _uploadFile, child: Text('Upload File'), ), ], ); } } ``` 您需要将“your-upload-api-url-here”替换为您的上传API的实际URL,“Bearer your_access_token_here”替换为您的实际访问令牌。此外,您需要在pubspec.yaml文件中添加依赖项file_picker和http。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A_Just_Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值