将图像文件上传到Firebase存储

将图像文件上传到Firebase存储

本文介绍了Flutter Web-将图像文件上传到Firebase存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter Web上,我从计算机中选择一个图像文件并获得一个File图像对象.然后,我想将其上传到Firebase存储.对于该应用程序的Android和iOS版本,我使用了Firebase Cloud Function和一个http multipart请求.它有效,但对于该应用程序的网络版本却无效.因此,

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,

如何直接或通过Cloud Function将html图像文件上传到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到"images"文件夹中的函数的代码.文件夹:

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存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:07