繁体   English   中英

flutter_downloader 完成后自动打开文件

[英]flutter_downloader auto open file when complete

所以我正在尝试使用 flutter_downloader package 在 flutter 中使用下载管理器。我设法让我的应用程序下载我想要的文件。 我怎样才能使我的应用程序更先进? 我希望我的应用程序能够在下载完成后自动打开下载的文件。

另一个问题是,如果我已经第一次下载了文件,我怎样才能让按钮触发打开文件位置而不是再次下载文件?

这是我当前的代码:

  TextButton(
    child: Text(
      ticketData['file_attachment_map'][index]['name'],
      style: primaryColor600Style.copyWith(fontSize:14.sp),
    ),
    
    onPressed: () async {
      final permissionStatus = await Permission.storage.request();
    
      if (permissionStatus.isGranted) {
      final externalDir = await getExternalStorageDirectory();
    
      final taskId =await FlutterDownloader.enqueue(
        url: ticketData['file_attachment_map']
        [index]['url'],
        savedDir:externalDir!.path,
        showNotification:true,
        openFileFromNotification:true,
        );
      } else {
        print('Permission denied');
      }
    },
  ),

下载后使用open_file package

if(taskId != null){
    await OpenFile.open(filePath);
  }
}

您可以在下载文件之前检查文件是否存在

import 'dart:io';
File("path/to/file").exists() 

作为对@Xuann Thucc 答案的补充,您可以使用open_filex ,它是open_file的一个分支,但得到了更好的维护,并修复了原始库的许多问题。

import 'package:open_filex/open_filex.dart';

// in async function
await OpenFilex.open(filePath);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM