我目前正在探索Flutter,我发现Flutter中有一个官方的Firebase Storage插件firebase_storage。我有一个类似这样的存储引用:
final StorageReference ref = FirebaseStorage.instance.ref().child("default.png");
但是没有一种方法可以从那个 StorageReference 获取下载 URL。
我目前正在探索Flutter,我发现Flutter中有一个官方的Firebase Storage插件firebase_storage。我有一个类似这样的存储引用:
final StorageReference ref = FirebaseStorage.instance.ref().child("default.png");
但是没有一种方法可以从那个 StorageReference 获取下载 URL。
Future<String> uploadImage(var imageFile ) async {
StorageReference ref = storage.ref().child("/photo.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
url = dowurl.toString();
return url;
}
使用以下格式的URL:
printUrl() async {
StorageReference ref =
FirebaseStorage.instance.ref().child("images/sky.jpg");
String url = (await ref.getDownloadURL()).toString();
print(url);
}
最新版本firebase_storage: ^5.0.1
应该采用以下方式:
Reference reference = FirebaseStorage.instance.ref().child("SupportChatImages").child(fileName);
UploadTask uploadTask = reference.putFile(imageFile);
uploadTask.whenComplete(() async{
try{
imageUrl = await reference.getDownloadURL();
}catch(onError){
print("Error");
}
print(imageUrl);
});
Future<List<String>> _uploadFiles(List<PickedFile> files) async {
在之前的版本中,由于for循环内部有"await",所以使用StorageTaskSnapshot snapshot = await uploadTask.onComplete;是可以正常工作的。 - Ishaan Puniani我已经实现了一种方法,可以将您的图像保存为带有时间戳的文件,并获得可下载的URL。
Future<String>photoOption() async {
try {
DateTime now = new DateTime.now();
var datestamp = new DateFormat("yyyyMMdd'T'HHmmss");
String currentdate = datestamp.format(now);
File imageFile = await ImagePicker.pickImage();
StorageReference ref = FirebaseStorage.instance
.ref()
.child("images")
.child("$currentdate.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
Uri downloadUrl = (await uploadTask.future).downloadUrl;
addUser.downloadablelink = downloadUrl.toString();
downloadableUrl = downloadUrl.toString();
print(downloadableUrl);
} catch (error) {
print(error);
}
return downloadableUrl;
}
put
已被弃用,请使用 putFile
。 - nonybrighto对于 firebase_storage: ^10.0.1:
以下是获取已上传图像URL的代码:
uploadImagetFirebase(String imagePath) async {
await FirebaseStorage.instance
.ref(imagePath)
.putFile(File(imagePath))
.then((taskSnapshot) {
print("task done");
// download url when it is uploaded
if (taskSnapshot.state == TaskState.success) {
FirebaseStorage.instance
.ref(imagePath)
.getDownloadURL()
.then((url) {
print("Here is the URL of Image $url");
return url;
}).catchError((onError) {
print("Got Error $onError");
});
}
});
}
Future<String> urlDownload(file) async {
var uuid = Uuid().v1();
StorageReference ref =
FirebaseStorage.instance.ref().child("post_$uuid.jpg");
StorageUploadTask uploadTask = ref.putFile(file);
String downloadUrl =
await (await uploadTask.onComplete).ref.getDownloadURL();
return downloadUrl;}
试试这个
Future<String> downloadFromFirebase() async {
// Create reference
StorageReference ref = FirebaseStorage.instance.ref().child("default.png");
String _myUrl = await ref.getDownloadURL();
return _myUrl.toString();
}
Future<String> uploadSingleImage(File file) async {
//Set File Name
String fileName = DateTime.now().millisecondsSinceEpoch.toString() +
AuthRepository.getUser().uid +
'.jpg';
//Create Reference
Reference reference = FirebaseStorage.instance
.ref()
.child('Single Post Images')
.child(fileName);
//Now We have to check the status of UploadTask
UploadTask uploadTask = reference.putFile(file);
String url;
await uploadTask.whenComplete(() async {
url = await uploadTask.snapshot.ref.getDownloadURL();
});
return url;
}
StorageReference storageReference = FirebaseStorage.instance.ref().child("myfile");
StorageUploadTask uploadTask = storageReference.putFile(file);
uploadTask.onComplete.then((s){
s.ref.getDownloadURL();
});
Future mainprofile(File image) async {
try {
DateTime now = new DateTime.now();
var datestamp = new DateFormat("yyyyMMdd'T'HHmmss");
String currentdate = datestamp.format(now);
_firebaseStorageRef = FirebaseStorage.instance
.ref()
.child(userRepository.uid)
.child('main')
.child("$currentdate.jpg");
StorageUploadTask uploadTask = _firebaseStorageRef.putFile(image);
uploadTask.onComplete.then((onValue) async {
_mainurl = (await _firebaseStorageRef.getDownloadURL()).toString();
});
} catch (error) {
print(error);
}
}
StorageReference
中获取下载路径。我认为Firebase Storage插件仍然缺少一些功能,无法像https://firebase.google.com/docs/storage/web/download-files中所示那样做某些事情。 - Günter Zöchbauer