繁体   English   中英

如何将pdf文件上传到firebase存储并获取flutter中的url?

[英]How to upload pdf file to firebase storage and get the url in flutter?

我尝试将 pdf 文件上传到FirebaseStorage ,然后下载 url。我正在使用file_picker: ^5.2.0+1来选择 pdf 文件。 问题是上传 firebase 存储,它告诉 FilePicker 无法分配文件。 我尝试了几种方法,但都行不通。

FilePickerResult? result = await FilePicker.platform.pickFiles();

          if (result != null) {
            Uint8List? fileBytes = result.files.first.bytes;
            print(fileBytes);
            String fileName = result.files.first.name;
            print(fileName);
            await FirebaseStorage.instance.ref('uploads/jk').putData(fileBytes);
          }

这将为您完成,从您正在使用的设备中选择您想要的文件,将其上传到 firebase 存储并在完成后下载 Url,假设您正在使用 statefulwidget

var url;
onTap() async {

//this will open up the device filePicker              
                File pdf;
                var pickedFile = await FilePicker.platform.pickFiles();

                if (pickedFile != null) {
                  pdf = File(pickedFile.files.single.path!);
///this will upload the pickedfile to firebase storage with the specified name in the .child() 
                  await FirebaseStorage.instance
                      .ref()
                      .child('pdf')
                      .putFile(pdf);
                }
///this part will fetch the download Url from firebase storage
///make sure to correctly specify the name of the file you want
                FirebaseStorage.instance
                    .ref()
                    .child('pdf')
                    .getDownloadURL()
                    .then((value) => setState(() {url = value;});         
              },

url 变量将获取从下载 url 文件的方法返回的值

暂无
暂无

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

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