Home > Software design >  Upload Blob Url to Firebase Storage | Flutter
Upload Blob Url to Firebase Storage | Flutter

Time:08-21

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:

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

blob: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. I thought that I have a Url and therefore should be able to upload it like this:

      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.

What can I do different to upload a blob successfully to firebase Storage?

-----------------Answer----------------------

Answer in the comment of the answer.

You have to convert your Blob to a Uint8List & upload it like:

 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;
}
}

CodePudding user response:

The Firebase Storage SDKs can upload local data as either a File, an array of bytes, or a base-64 encoded string. The only URLs it accepts are so-called data URLs, which start with data:// and contain the complete data of the object. They cannot upload data directly from URLs that you more commonly see, such as http:// or https://.

You'll need to first download the data from that URL to the local device, and then upload it from there.

  • Related