Skip to content

Blob download not working as before #295

Open
@Cunibon

Description

@Cunibon

I am trying to update my app from dart:html to package:web

I had this function:

Future<String?> downloadData(Uint8List data, {required String filename}) async {
  final blob = html.Blob([data], 'text/plain', 'native');
  html.AnchorElement(href: html.Url.createObjectUrlFromBlob(blob))
    ..setAttribute('download', filename)
    ..click()
    ..remove();
  return null;
}

and updated it to this:

Future<String?> downloadData(Uint8List data, {required String filename}) async {
  final blob = web.Blob(
    [data.buffer.toJS].toJS,
    web.BlobPropertyBag(type: 'text/plain', endings: 'native'),
  );
  web.HTMLAnchorElement()
    ..href = web.URL.createObjectURL(blob)
    ..setAttribute('download', filename)
    ..click()
    ..remove();
  return null;
}

But the excel I want to download says it is corrupted and needs to be fixed to work.

I took a look at the resulting excel file in VSCode and found that there are subtle changes to some of the bytes randomly every time I run it. With the same code and the same data

For example the start line:

  1. "PK��������&P�Y�bi�������������xl/drawings/drawing1.xml��Kn�0������������F�6�'(��ړ��hƔp�ZP�RY�XZ������zt��Bb�|#�i)"
  2. "PK��������5P�Y�bi�������������xl/drawings/drawing1.xml��Kn�0������������F�6�'(��ړ��hƔp�ZP�RY�XZ������zt��Bb�|#�i)"

You can see that "&P" turned into "5P"

Additionally the file size increased 7x from 5kb to 36kb. This is due to the file ending in 27777 additional null characters

I attached both the the broken and the legit version as excel files
broken.xlsx
legit.xlsx

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions