简体   繁体   English

如何用flutter上传pdf到firebase存储?

[英]How to upload pdf to firebase storage with flutter?

I'm using latest firebase_storage version now some methods and references of storage are updated ploadTask.onComplete does not support.我正在使用最新的firebase_storage版本,现在一些方法和存储参考已更新ploadTask.onComplete不支持。

so how do I get uploaded pdf url?那么如何上传 pdf url?

Piece of code:一段代码:

 Future<String> uploadPdfToStorage(File pdfFile) async {
    try {
      Reference ref = FirebaseStorage.instance.ref().child('pdfs/${DateTime.now().millisecondsSinceEpoch}');
    UploadTask uploadTask = ref.putFile(pdfFile, SettableMetadata(contentType: 'pdf')); 
    String downloadUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
  


   final String url = await downloadUrl;


  print("url:$url");
  return  url;
    } catch (e) {
      return null;
    }
  }

Here's an edited code that should work with the latest firebase storage_package :这是经过编辑的代码,应该适用于最新的 firebase storage_package

Future<String> uploadPdfToStorage(File pdfFile) async {
  try {
    Reference ref =
        FirebaseStorage.instance.ref().child('pdfs/${DateTime.now().millisecondsSinceEpoch}');
    UploadTask uploadTask = ref.putFile(pdfFile, SettableMetadata(contentType: 'pdf'));

    TaskSnapshot snapshot = await uploadTask;

    String url = await snapshot.ref.getDownloadURL();

    print("url:$url");
    return url;
  } catch (e) {
    return null;
  }
}

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

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