I have a problem with saving data to the device. I have base64 String that I need to save to the device as an excel file. Everything happens on web. All I found so far are mobile solutions that doesn't work.
Could you please help :)
This is the code that I have currently. It saves the file, however it I can't open it afterwards so there has to be something wrong on the way. Since this solution writes the file I'm guessing I'm converting something wrongly.
    //base64StringFile is the file I'm getting from the web. It's a String
    Uint8List bytes = base64.decode(base64StringFile);
    File file = File.fromRawPath(bytes);
    final blob = html.Blob([file]);
    final url = html.Url.createObjectUrlFromBlob(blob);
    final anchor = html.document.createElement('a') as html.AnchorElement
      ..href = url
      ..style.display = 'none'
      ..download = 'myFileName';
    html.document.body.children.add(anchor);
    anchor.click();
    html.document.body.children.remove(anchor);
    html.Url.revokeObjectUrl(url);
File.fromRawPath interprets the given bytes as path information relating to the filesystem - it's not relevant here.
Instead, create your blob with the bytes directly like so, and don't use a File object.
Note that the byte list is enclosed in another list.
Additionally, you should specify the Excel mime type when doing so.
final blob = html.Blob([bytes], 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With