web icon indicating copy to clipboard operation
web copied to clipboard

Blob download not working as before

Open Cunibon opened this issue 5 months ago • 2 comments

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���&PYbi��������xl/drawings/drawing1.xml��Kn�0��������F6�'(�ړ�hƔp�ZP�RY�XZ������zt��Bb|#�i)"
  2. "PK���5PYbi��������xl/drawings/drawing1.xml��Kn�0��������F6�'(�ړ�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

Cunibon avatar Aug 29 '24 08:08 Cunibon