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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Document type not recognized by browser if using string template literal as file name #2072

Closed
wpyoga opened this issue Jun 6, 2022 · 0 comments

Comments

@wpyoga
Copy link

wpyoga commented Jun 6, 2022

馃悰 Bug Report

When I use a string template literal as file name, the browser downloads the file as a binary blob:

documentDate = '2022-06-06';
documentTitle = 'Hello World';
anchor.download = `${documentDate} ${documentTitle}.xslx`;

It works when I use simple string concatenation

documentDate = '2022-06-06';
documentTitle = 'Hello World';
anchor.download = documentDate + " " + documentTitle + ".xslx";

Lib version:
ExcelJS: 4.3.0
React: 18.1.0
TypeScript: 4.7.3

Browsers tested:
Chrome 102.0.5005.61
Firefox 100.0.2

All tests done on Linux (Intel 64-bit).

Steps To Reproduce

import ExcelJS from 'exceljs';
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');

worksheet.getCell('A1').value = 'Hello World';

workbook.xlsx.writeBuffer().then((buffer) => {
  const blob = new Blob([buffer], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  });
  const url = window.URL.createObjectURL(blob);
  const anchor = document.createElement('a', {});
  anchor.href = url;

  const documentDate = '2022-06-06';
  const documentTitle = 'Hello World';

  anchor.download = `${documentDate} ${documentTitle}.xslx`;
  // the following results in normal behaviour
  // anchor.download = documentDate + " " + documentTitle + ".xslx";

  anchor.click();
  window.URL.revokeObjectURL(url);
});

Adapt the above code to a button's onClick handler, and then click the button to see it in action.

The expected behaviour:

The file is downloaded and recognized to be an Excel file, such that the user can click on the file in their download history, and have the associated program (MS Excel, or LibreOffice Calc) open it.

Notes

Note that I'm using TypeScript, but I've inspected the generated JavaScript and didn't see any changes to the anchor.download line.

The above code was originally based on a comment by @dlinx in #354

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants