简体   繁体   English

flutter_downloader 完成后自动打开文件

[英]flutter_downloader auto open file when complete

so I'm trying to work with download manager in flutter using flutter_downloader package. I manage to make my app download the file I wanted.所以我正在尝试使用 flutter_downloader package 在 flutter 中使用下载管理器。我设法让我的应用程序下载我想要的文件。 How can I make my app more advance?我怎样才能使我的应用程序更先进? I wanted my app to be able to open the downloaded file automatically when the download is done.我希望我的应用程序能够在下载完成后自动打开下载的文件。

Another question is, if I already download the file first time how can I make the button trigger to open the file location rather than downloading the file again?另一个问题是,如果我已经第一次下载了文件,我怎样才能让按钮触发打开文件位置而不是再次下载文件?

Here's my current code:这是我当前的代码:

  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');
      }
    },
  ),

Use open_file package after downloaded下载后使用open_file package

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

And you can check file exist before download file您可以在下载文件之前检查文件是否存在

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

As a complement to @Xuann Thucc answer, you can use the open_filex which is a fork of open_file but is better maintained and fixes many issues with the original lib.作为对@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