Flutter Flatter web-将图像上载到firebase存储
我正在使用Flatter web上的Flutter Flatter web-将图像上载到firebase存储,flutter,firebase-storage,flutter-web,Flutter,Firebase Storage,Flutter Web,我正在使用Flatter web上的firebase\u存储:^8.0.6软件包。我想将图像上传到使用FilePickerpackage获取的firebase存储。 问题是新包使用putFile()方法上载文件。但是来自dart:io的文件在flatterweb上不起作用,它也不接受来自dart:html的文件对象 我可以使用putBlob()方法将图像上载为Blob,但它不会将其上载为图像类型,但其类型为application/octet stream。我不想将图像文件作为blob上传 F
firebase\u存储:^8.0.6
软件包。我想将图像上传到使用FilePicker
package获取的firebase存储。
问题是新包使用putFile()方法上载文件。但是来自dart:io的文件在flatterweb上不起作用,它也不接受来自dart:html的文件对象
我可以使用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 ';
}
}
Future uploadImage(PlatformFile文件)异步{
试一试{
TaskSnapshot upload=等待FirebaseStorage.instance
参考号(
'events/${file.name}-${DateTime.now().toIso8601String()}.${file.extension}')
.putBlob(Blob(file.bytes));
字符串url=wait upload.ref.getDownloadURL();
返回url;
}捕获(e){
print(${e.toString()}上传图像时出错);
返回';
}
}
如何解决此问题?当您使用
File.fromUri()
构造函数时,仍然可以使用.putFile
,并使用Uri.dataFromBytes
从平台文件
对象获取Uri
,并将字节传递给它
下面的代码包含应删除错误的更改:
TaskSnapshot upload=等待FirebaseStorage.instance
参考号(
'events/${file.name}-${DateTime.now().toIso8601String()}.${file.extension}')
.putFile(File.fromUri(Uri.dataFromBytes(File.bytes.toList()));
您可以使用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 '';
}
}
Future uploadImage(PlatformFile文件)异步{
试一试{
TaskSnapshot upload=等待FirebaseStorage.instance
参考号(
'events/${file.path}-${DateTime.now().toIso8601String()}.${file.extension}')
.putData(
file.bytes,
SettableMetadata(contentType:'image/${file.extension}'),
);
字符串url=wait upload.ref.getDownloadURL();
返回url;
}捕获(e){
print(${e.toString()}上传图像时出错);
返回“”;
}
}
putData()方法默认采用Uint8List。这是否回答了您的问题@DarShan这个问题使用put,我使用的是putFile,它接受dart而不是universal_html的文件。嘿,它不工作。“不支持的操作:无法从数据URI提取文件路径”-这是我得到的错误。dart:io不适用于基于浏览器的应用程序。这就是为什么我不能使用File对象。