可以在Flutter中使用文件扩展名(.pdf或.jpg)将文件上传到Firebase吗?

huangapple go评论61阅读模式
英文:

Can we upload file to firebase with file extension in flutter(.pdf or .jpg)?

问题

以下是您提供的文本的中文翻译:

我提出这个问题的主要原因是,当我正常上传图像或文件时,URL看起来像这样...

https://firebasestorage.googleapis.com/v0/b/petpulz-93d6f.appspot.com/o/records%2Fpost_1683391240123?alt=media&token=3fa03aa4-78f4-4329-bacf-5793072055e1

由于这些URL,当我获取、重新打开或下载文件时,无法识别其文件类型。
是否有一种方法可以将文件上传到Firebase,并在URL的末尾添加相关的文件扩展名,例如**.pdf**、.jpg或**.doc**?

上传到Firebase存储并将下载URL上传到Firebase Firestore

  Future<void> _uploadFiles() async {
    final postID = DateTime.now().millisecondsSinceEpoch.toString();
    final storage = FirebaseStorage.instance;
    final firestore = FirebaseFirestore.instance;

    final List<String> downloadUrls = [];

    for (final file in _selectedFiles) {
      final ref = storage.ref().child("user").child("post_$postID");
      final uploadTask = ref.putFile(file);

      final snapshot = await uploadTask.whenComplete(() {});

      final downloadUrl = await snapshot.ref.getDownloadURL();
      downloadUrls.add(downloadUrl);
    }

    await firestore.collection("users")
        .doc(user?.uid)
        .set({
          "username": title.text,
          'fileUrls': FieldValue.arrayUnion(downloadUrls),
        });

    Navigator.of(context).push(MaterialPageRoute(builder: (context) => HomeScreen()));
  }

请注意,上述代码的注释和标记没有被翻译。如果您需要进一步的翻译或有其他问题,请告诉我。

英文:

The main reason for my question is that when I normally upload an image or file, the URL looks like this..

https://firebasestorage.googleapis.com/v0/b/petpulz-93d6f.appspot.com/o/records%2Fpost_1683391240123?alt=media&amp;token=3fa03aa4-78f4-4329-bacf-5793072055e1

Because of URLs like these, when I fetch, reopen, or download the file, I cannot identify its file type.
Is there a way to upload files to Firebase and add the relevant file extension at the end of the URL, such as .pdf, .jpg, or .doc

upload firebase storage and upload downloadurl to firebase firestore

  Future&lt;void&gt; _uploadFiles() async {
    final postID = DateTime.now().millisecondsSinceEpoch.toString();
    final storage = FirebaseStorage.instance;
    final firestore = FirebaseFirestore.instance;

    final List&lt;String&gt; downloadUrls = [];

    for (final file in _selectedFiles) {
      final ref = storage.ref().child(&quot;user&quot;).child(&quot;post_$postID&quot;);
      final uploadTask = ref.putFile(file);

      final snapshot = await uploadTask.whenComplete(() {});

      final downloadUrl = await snapshot.ref.getDownloadURL();
      downloadUrls.add(downloadUrl);
    }

    await firestore.collection(&quot;users&quot;)
        .doc(user?.uid)
        .set({
    &quot;username&quot; : title.text,
      &#39;fileUrls&#39;: FieldValue.arrayUnion(downloadUrls),
    });

    Navigator.of(context).push(MaterialPageRoute(builder: (context) =&gt; HomeScreen()));
  }

答案1

得分: 1

You can get the extension of the file using path package:

import 'package:path/path.dart' as p;

...

for (final file in _selectedFiles) {
  // add this the get the extension
  final fileExtension = p.extension(file.path);  

  // append the extension to the child name
  final ref = storage.ref().child("user").child("post_$postID$fileExtension");
  final uploadTask = ref.putFile(file);

  final snapshot = await uploadTask.whenComplete(() {});

  final downloadUrl = await snapshot.ref.getDownloadURL();
  downloadUrls.add(downloadUrl);
}
英文:

You can get the extension of the file using path package:

import &#39;package:path/path.dart&#39; as p;

...

for (final file in _selectedFiles) {
  // add this the get the extension
  final fileExtension = p.extension(file.path);  

  // append the extension to the child name
  final ref = storage.ref().child(&quot;user&quot;).child(&quot;post_$postID$fileExtension&quot;);
  final uploadTask = ref.putFile(file);

  final snapshot = await uploadTask.whenComplete(() {});

  final downloadUrl = await snapshot.ref.getDownloadURL();
  downloadUrls.add(downloadUrl);
}

huangapple
  • 本文由 发表于 2023年5月7日 01:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定