Arb*_*had 5 flutter firebase-storage flutter-web
我正在firebase_storage: ^8.0.6flutter web 上使用该包。我想将图像上传到我使用FilePickerpackage.json 获得的 firebase 存储。问题是新包使用 putFile() 方法上传文件。但是 dart:io 中的 File 不适用于 flutter web,并且它也不接受 dart:html 中的 File 对象。
我可以使用 putBlob() 方法将图像上传为 Blob,但它不会将其作为图像类型上传,但它的类型是application/octet-stream. 我不想将图像文件作为 blob 上传。
Future<String> uploadImage(PlatformFile file) async {
try {
TaskSnapshot upload = await FirebaseStorage.instance
.ref(
'events/${file.name}-${DateTime.now().toIso8601String()}.${file.extension}')
.putBlob(Blob(file.bytes));
String url = await upload.ref.getDownloadURL();
return url;
} catch (e) {
print('error in uploading image for : ${e.toString()}');
return ';
}
}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
Arb*_*had 13
您可以使用 putData() 方法发送图像并将其元数据设置为图像。
Future<String> uploadImage(PlatformFile file) async {
try {
TaskSnapshot upload = await FirebaseStorage.instance
.ref(
'events/${file.path}-${DateTime.now().toIso8601String()}.${file.extension}')
.putData(
file.bytes,
SettableMetadata(contentType: 'image/${file.extension}'),
);
String url = await upload.ref.getDownloadURL();
return url;
} catch (e) {
print('error in uploading image for : ${e.toString()}');
return '';
}
}
Run Code Online (Sandbox Code Playgroud)
putData() 方法默认采用 Uint8List。
| 归档时间: |
|
| 查看次数: |
3812 次 |
| 最近记录: |