如何在 flutter web 中将 XFile 图像上传到 Firebase 存储?
How to Upload XFile image to Firebase Storage in flutter web?
适用于移动设备的代码。
final profileImageFile = await _profileImage.readAsBytes();
if (!kIsWeb) {
try {
await ref
.putData(
profileImageFile,
fbSorage.SettableMetadata(
contentType: 'image/png',
),
)
.whenComplete(() {});
} on Exception catch (e) {
print(e);
return;
}
}
_profileImage
是 XFile
在网络上尝试时,上传的文件已损坏。
The following DomException object was thrown resolving an image codec:
EncodingError: The source image cannot be decoded.
我使用 universal_html
包上传 html.Blob
使用 putBlob
方法:
if (kIsWeb) {
await ref
.putBlob(
html.Blob(profileImageFile),
fbSorage.SettableMetadata(
contentType: 'image/png',
),
)
.whenComplete(() {});
}
抛出相同的异常。
我遇到了同样的问题,这个 link 帮助了我:
https://pub.dev/packages/firebase_storage/example
只需设置元数据:
final metadata = SettableMetadata(
contentType: 'image/jpeg',
customMetadata: {'picked-file-path': file.path},
);
if (kIsWeb) {
uploadTask = ref.putData(await file.readAsBytes(), metadata);
} else {
uploadTask = ref.putFile(io.File(file.path), metadata);
}
适用于移动设备的代码。
final profileImageFile = await _profileImage.readAsBytes();
if (!kIsWeb) {
try {
await ref
.putData(
profileImageFile,
fbSorage.SettableMetadata(
contentType: 'image/png',
),
)
.whenComplete(() {});
} on Exception catch (e) {
print(e);
return;
}
}
_profileImage
是 XFile
在网络上尝试时,上传的文件已损坏。
The following DomException object was thrown resolving an image codec:
EncodingError: The source image cannot be decoded.
我使用 universal_html
包上传 html.Blob
使用 putBlob
方法:
if (kIsWeb) {
await ref
.putBlob(
html.Blob(profileImageFile),
fbSorage.SettableMetadata(
contentType: 'image/png',
),
)
.whenComplete(() {});
}
抛出相同的异常。
我遇到了同样的问题,这个 link 帮助了我: https://pub.dev/packages/firebase_storage/example 只需设置元数据:
final metadata = SettableMetadata(
contentType: 'image/jpeg',
customMetadata: {'picked-file-path': file.path},
);
if (kIsWeb) {
uploadTask = ref.putData(await file.readAsBytes(), metadata);
} else {
uploadTask = ref.putFile(io.File(file.path), metadata);
}