英文:
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&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<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()));
}
答案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 '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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论