Future<String?> uploadImageOld () async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
final storageRef = FirebaseStorage.instance.ref();
final images = storageRef.child('images');
File file = File(result.files.single.path!);
final filename = Path.basename(file.path);
final uploadRef = images.child('images/$filename');
await uploadRef.putFile(file);
return await uploadRef.getDownloadURL();
} else {
return null;
}
}
尝试选择一个文件并上传到 Firebase 云存储,但在选择文件后它冻结了。
看看是否有效
Future<String?> uploadImageOld () async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
final metadata = SettableMetadata(contentType: "image/jpeg");// pass other meta data if needed
final storageRef = FirebaseStorage.instance.ref();
// final images = storageRef.child('images');
File file = File(result.files.single.path!);
final filename = Path.basename(file.path);
final uploadRef = storageRef.child('images/$filename');
final uploadTask =await uploadRef.putFile(file);
uploadTask.snapshotEvents.listen((TaskSnapshot taskSnapshot) {
switch (taskSnapshot.state) {
case TaskState.running:
final progress =
100.0 * (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes);
print("Upload is $progress% complete.");
break;
case TaskState.paused:
print("Upload is paused.");
break;
case TaskState.canceled:
print("Upload was canceled");
break;
case TaskState.error:
// Handle unsuccessful uploads
break;
case TaskState.success:
return await uploadRef.getDownloadURL();
// Handle successful uploads on complete
// ...
break;
}
});
//return await uploadRef.getDownloadURL();
} else {
return null;
}
}