Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blob download not working as before #295

Open
Cunibon opened this issue Aug 29, 2024 · 3 comments
Open

Blob download not working as before #295

Cunibon opened this issue Aug 29, 2024 · 3 comments
Labels
type-question A question about expected behavior or functionality

Comments

@Cunibon
Copy link

Cunibon commented Aug 29, 2024

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

@srujzs srujzs added the type-question A question about expected behavior or functionality label Sep 3, 2024
@srujzs
Copy link
Contributor

srujzs commented Sep 3, 2024

Can you share a repro where you create the Uint8List and call downloadData?

The only difference in calls that I can tell is that in the package:web example, you are calling toJS on the ArrayBuffer instead of on the view i.e. [data.buffer.toJS].toJS instead of [data.toJS].toJS. Maybe that shouldn't make a difference but I don't know how Blob interprets that.

@ykmnkmi
Copy link

ykmnkmi commented Sep 3, 2024

You can also use Uri.dataFromBytes:

void downloadData(Uint8List data, {required String filename}) {
  web.HTMLAnchorElement()
    ..href = UriData.fromBytes(data, mimeType: 'text/plain').toString()
    ..setAttribute('download', filename)
    ..click()
    ..remove();
}

Is using text/plain format and setting endings for xlsx file right? Not application/vnd.openxmlformats-officedocument.spreadsheetml.sheet?

@Trung15010802
Copy link

Same, I can't download image anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

4 participants