简体   繁体   English

将 Blob Url 上传到 Firebase 存储 | Flutter

[英]Upload Blob Url to Firebase Storage | Flutter

So I already read about the topic but I simply didn't understand the solutions on stack.因此,我已经阅读了有关该主题的内容,但我根本不了解堆栈上的解决方案。

I came up with this code:我想出了这段代码:

Im saving a url looking like this:我保存 url 看起来像这样:

final String myDataUrl = file.url;
print(myDataUrl);

blob:http://localhost:51947/2952a3b1-db6a-4882-a42a-8e1bf0a0ad73斑点:http://localhost:51947/2952a3b1-db6a-4882-a42a-8e1bf0a0ad73

& then Im trying to add it into Firebase Storage with the putString operator, that I guessed that suited me best while reading the Documentation. & 然后我尝试使用 putString 运算符将它添加到 Firebase 存储中,我猜这在阅读文档时最适合我。 I thought that I have a Url and therefore should be able to upload it like this:我以为我有一个 Url 因此应该能够像这样上传它:

      FirebaseStorage.instance
      .ref()
      .child("bla")
      .putString(myDataUrl, format: PutStringFormat.dataUrl);

But it doesn't work, it says that:但它不起作用,它说:

Error: Invalid argument (uri): Scheme must be 'data': Instance of '_Uri'

So Im guessing that it somehow can't format my url to one that is accepted.所以我猜它无法将我的 url 格式化为可以接受的格式。

What can I do different to upload a blob successfully to firebase Storage?为了将 blob 成功上传到 firebase 存储,我可以做些什么不同的事情?

-----------------Answer---------------------- - - - - - - - - -回答 - - - - - - - - - - -

Answer in the comment of the answer.在答案的评论中回答。

You have to convert your Blob to a Uint8List & upload it like:您必须将您的 Blob 转换为 Uint8List 并像这样上传:

 Future<Uint8List> fileConverter() async {
final reader = html.FileReader();
reader.readAsArrayBuffer(file!);
await reader.onLoad.first;
return reader.result as Uint8List;
 }

and then put it into your Storage:然后将其放入您的存储中:

  Future uploadFile(String uid) async {
if (file == null) return;
final path = "nachweise/$uid";
Uint8List fileConverted = await fileConverter();
try {
  FirebaseStorage.instance
      .ref()
      .child(path)
      .putData(fileConverted)
      .then((bla) => print("sucess"));
} on FirebaseException catch (e) {
  return null;
}
}

The Firebase Storage SDKs can upload local data as either a File , an array of bytes , or a base-64 encoded string . Firebase 存储 SDK 可以将本地数据上传为文件字节数组base-64 编码字符串 The only URLs it accepts are so-called data URLs , which start with data:// and contain the complete data of the object.它接受的唯一 URL 是所谓的数据 URL ,它以data://开头并包含 object 的完整数据。 They cannot upload data directly from URLs that you more commonly see, such as http:// or https:// .他们无法直接从您更常见的 URL 上传数据,例如http://https://

You'll need to first download the data from that URL to the local device, and then upload it from there.您需要先从 URL 下载数据到本地设备,然后从那里上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM