简体   繁体   English

如何在flutter中将文件从firebase下载到本地手机存储?

[英]How to download file from firebase to local phone storage in flutter?

I am new to flutter and I am clueless on how to implement a function to download a PDF file from firebase.我是 flutter 的新手,我对如何实现从 firebase 下载 PDF 文件的功能一无所知。 I went through multiple tutorials regarding downloading files from cloud storage to local storage in flutter app and still unable to make it work.我浏览了多个关于在 flutter 应用程序中将文件从云存储下载到本地存储的教程,但仍然无法使其工作。

I want when user presses the button我想当用户按下按钮时

 onPressed: () async {},

Then, it will straight away download the file from my firebase cloud storage to their local files.然后,它会立即将文件从我的 firebase 云存储下载到他们的本地文件。

I know I have to use writeToFile, but how do I implement the function?我知道我必须使用 writeToFile,但是如何实现该功能?

Hope you can help me, thanks!希望您能帮帮我,谢谢!

When looking at the docs for Firebase Cloud Storage, you can download a file from the Cloud Storage to the local documents folder, by using the following function:查看 Firebase Cloud Storage 的文档时,您可以使用以下函数将文件从 Cloud Storage 下载到本地文档文件夹:

//You'll need the Path provider package to get the local documents folder.
import 'package:path_provider/path_provider.dart';

Future<void> downloadFileExample() async {
  //First you get the documents folder location on the device...
  Directory appDocDir = await getApplicationDocumentsDirectory();
  //Here you'll specify the file it should be saved as
  File downloadToFile = File('${appDocDir.path}/downloaded-pdf.pdf');
  //Here you'll specify the file it should download from Cloud Storage
  String fileToDownload = 'uploads/uploaded-pdf.pdf';

  //Now you can try to download the specified file, and write it to the downloadToFile.
  try {
    await firebase_storage.FirebaseStorage.instance
        .ref(fileToDownload)
        .writeToFile(downloadToFile);
  } on firebase_core.FirebaseException catch (e) {
    // e.g, e.code == 'canceled'
    print('Download error: $e');
  }
}

You can find this, and read more about it on this page: https://firebase.flutter.dev/docs/storage/usage你可以找到这个,并在这个页面上阅读更多关于它的信息: https : //firebase.flutter.dev/docs/storage/usage

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

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