Reputation: 786
I have created a button to download the file from the URL I get from firestore, I need the file name too, is there any way to reference the file using the URL I get? I saw answers to similar questions but couldn't find the proper solution.
This is the type of URL I get
https://firebasestorage.googleapis.com/v0/b/student-app.appspot.com/o/img%2FFileName.pdf?alt=media&token=367f598b-425d-4ce0-be2b-804d2103d69e
is there any way to extract the file name from it? or by any reference method?
Upvotes: 0
Views: 2070
Reputation: 1
there is a simple solution : we suppose the url from fireStore :
final urlExample = https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/example_images%2F12345%2Fexample.jpg?alt=media&token=3ae5accd-ebdc-4fab-a08e-d8d57059614c
String getFileName(String url) {
Uri uri = Uri.parse(url);
String fileName = uri.pathSegments.last.split("/").last;
return fileName;
}
final fileNameFromUrl = getFileName(urlExample);
print(fileNameFromUrl); // example.jpg
Upvotes: 0
Reputation: 50920
If you don't have the reference to file then you would have to use Regex:
String getFileName(String url) {
RegExp regExp = new RegExp(r'.+(\/|%2F)(.+)\?.+');
//This Regex won't work if you remove ?alt...token
var matches = regExp.allMatches(url);
var match = matches.elementAt(0);
print("${Uri.decodeFull(match.group(2)!)}");
return Uri.decodeFull(match.group(2)!);
}
Upvotes: 10