Skip to content

Suggestion: Create util for downloading CSV (and maybe similar stuff) #4

@MB-web

Description

@MB-web

We have this in some projects:

`export type Row = (string | number)[];
export type Payload = Row[];

/**

  • Handles downloading an array as CSV list.
    */
    export default function downloadCsv(payload: Payload, fileName: string, delimiter = ','): void {
    const content = \ufeff${payload.map(row => row.join(delimiter)).join('\n')};
    const anchorElement = document.createElement('a');
    const mimeType = 'text/csv;encoding:utf-8';

anchorElement.href = URL.createObjectURL(new Blob([content], {
type: mimeType,
}));
anchorElement.setAttribute('download', fileName);
document.body.appendChild(anchorElement);
anchorElement.click();
document.body.removeChild(anchorElement);
}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions