作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 Flutter web 并将图像上传到 Firestore 时遇到问题。我很确定问题出在图像选择器中,作为正常(移动)image picker不适用于网络。普通图像选择器返回一个文件,但替代 image_picker_web返回一个图像,它在上传时被拒绝,因为它期待一个 Future<File>
.
image_picker_web有一个替代方法可以返回 Uint8List
我使用过,然后转换为 File
通过 dart:html
- 上传正常,但图像已损坏且无法查看。
这是我所做的:
按下按钮 - 选择图像为 Uint8List
> 转换为 Image
, 存储在内存中并显示在屏幕上
onPressed: () async {
//Upload Image as Uint8List
imageBytes = await ImagePickerWeb.getImage(asUint8List: true);
//Convert Uint8List to Image
_image = Image.memory(imageBytes);
//Show new image on screen
setBottomSheetState(() {
image = _image;
});
},
Uint8List
至
File
使用
dart:html
File
并命名为用户 UID.png(PNG 上传)
imageFile = html.File(imageBytes, '${user.uid}.png');
import 'dart:async';
import 'package:firebase/firebase.dart' as fb;
import 'package:universal_html/prefer_universal/html.dart' as html;
String url;
Future<String> uploadProfilePhoto(html.File image, {String imageName}) async {
try {
//Upload Profile Photo
fb.StorageReference _storage = fb.storage().ref('profilephotos/$imageName.png');
fb.UploadTaskSnapshot uploadTaskSnapshot = await _storage.put(image).future;
// Wait until the file is uploaded then store the download url
var imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
url = imageUri.toString();
} catch (e) {
print(e);
}
return url;
}
location = await uploadProfilePhoto(imageFile, imageName: '${user.uid}');
//Pass new user ID through to users Collection to link UserData to this user
await AdminUserData(uid: user.uid).updateAdminUserData(name: userName, email: userEmail, profilephoto: location);
最佳答案
我还没有尝试过你提到的替代方案,但下面在 Flutter web 和 Firebase 上对我有用。 uploadInput 的事件监听器适用于大多数平台。关于 document.body.append 的最后一部分将确保它也适用于 Mobile safari。
Future<void> _setImage() async {
final completer = Completer<String>();
InputElement uploadInput = FileUploadInputElement();
uploadInput.multiple = false;
uploadInput.accept = 'image/*';
uploadInput.click();
uploadInput.addEventListener('change', (e) async {
// read file content as dataURL
final files = uploadInput.files;
Iterable<Future<String>> resultsFutures = files.map((file) {
final reader = FileReader();
reader.readAsDataUrl(file);
reader.onError.listen((error) => completer.completeError(error));
return reader.onLoad.first.then((_) => reader.result as String);
});
final results = await Future.wait(resultsFutures);
completer.complete(results[0]);
});
document.body.append(uploadInput);
final String image = await completer.future;
widget.newImage = uploadInput.files[0];
// Upload to Firebase
uploadToFirebase(widget.newImage); // This is dart:html File
uploadInput.remove();
}
然后上传到 Firebase 存储:
uploadToFirebase(String imageName, File file) async {
Firebase.UploadTask task = storage.refFromURL('gs://.../images/' + imageName).put(file);
}
关于firebase - Flutter Web 上传到 Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60719724/
我是一名优秀的程序员,十分优秀!