无法从 flutter web 将图像上传到 Firebase Storage
Can't upload image to Firebase Storage from flutter web
所以我试过这个来获取图像并上传它:
var file;
html.InputElement uploadInput = html.FileUploadInputElement()
..accept = "image/*";
uploadInput.click();
uploadInput.onChange.listen((event) {
file = uploadInput.files.first;
final reader = html.FileReader();
reader.readAsDataUrl(file);
reader.onLoadEnd.listen((event) async {
print("done");
StorageReference storageReference = FirebaseStorage.instance
.ref()
.child('${widget.uid}/${DateTime.now()}');
StorageUploadTask uploadTask = storageReference.putData(file);
await uploadTask.onComplete;
print('File Uploaded');
print(storageReference.path);
storageReference.getDownloadURL().then((fileURL) {
setState(() {
_uploadedFileURL = fileURL;
});
});
});
});
如教程所示,但是这个人使用 .put() 而不是 putData() 但它现在已被弃用,所以在使用 .put() 时我得到:
Expected a value of type 'File', but got one of type 'File$'
使用 .putData() 我得到:
Expected a value of type 'Uint8List', but got one of type 'File$'
关于如何从 Flutter Web 用户转换 File$ 或直接导入 File 或 Uint8List 的任何想法?
谢谢
试试 reader.readAsArrayBuffer(file)
和 storageReference.putData(reader.result)
。
所以我试过这个来获取图像并上传它:
var file;
html.InputElement uploadInput = html.FileUploadInputElement()
..accept = "image/*";
uploadInput.click();
uploadInput.onChange.listen((event) {
file = uploadInput.files.first;
final reader = html.FileReader();
reader.readAsDataUrl(file);
reader.onLoadEnd.listen((event) async {
print("done");
StorageReference storageReference = FirebaseStorage.instance
.ref()
.child('${widget.uid}/${DateTime.now()}');
StorageUploadTask uploadTask = storageReference.putData(file);
await uploadTask.onComplete;
print('File Uploaded');
print(storageReference.path);
storageReference.getDownloadURL().then((fileURL) {
setState(() {
_uploadedFileURL = fileURL;
});
});
});
});
如教程所示,但是这个人使用 .put() 而不是 putData() 但它现在已被弃用,所以在使用 .put() 时我得到:
Expected a value of type 'File', but got one of type 'File$'
使用 .putData() 我得到:
Expected a value of type 'Uint8List', but got one of type 'File$'
关于如何从 Flutter Web 用户转换 File$ 或直接导入 File 或 Uint8List 的任何想法? 谢谢
试试 reader.readAsArrayBuffer(file)
和 storageReference.putData(reader.result)
。