问题描述
在 flutter web 上,我从计算机中选取一个图像文件并获取一个 File 图像对象.然后我想将它上传到 Firebase 存储.对于 Android 和 iOS 版本的应用程序,我使用了 Firebase Cloud Function 和 http 多部分请求.它有效,但对于该应用程序的网络版本则无效.所以,
On flutter web, I pick an image file from the computer and get a File image object. Then I want to upload it to Firebase Storage. For Android and iOS versions of the app I was using a Firebase Cloud Function and an http multipart request. It worked, but for the web version of the app it doesn't. So,
如何将 html 图像文件直接或通过 Cloud Functions 上传到 Firebase Storage?
How can I upload an html image file to Firebase Storage, either directly or through a Cloud Function?
推荐答案
最后我找到了解决这个问题的方法.为此,我需要安装两个依赖项,firebase 和 universal_html.虽然很难找到解决方案,但它的实现其实很简单.这是我用来将 html 图像文件上传到 Firebase Storage 的函数代码,到图像"文件夹:
Finally I managed to find a solution to this issue. To achieve this I needed to install two dependencies, firebase and universal_html.Yet difficult to find out the solution, its implementation was actually simple. Here is the code of the function I used to upload the html image file to Firebase Storage, into the "images" folder:
import 'dart:async';
import 'package:universal_html/prefer_universal/html.dart' as html;
import 'package:firebase/firebase.dart' as fb;
Future<Uri> uploadImageFile(html.File image,
{String imageName}) async {
fb.StorageReference storageRef = fb.storage().ref('images/$imageName');
fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(image).future;
Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
return imageUri;
}
我希望它可以帮助和我有同样需求的人.
I hope it helps someone with the same need as me.
这篇关于Flutter web - 将图像文件上传到 Firebase 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!