This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add CSV export functionality
- Loading branch information
1 parent
0973d61
commit e786d18
Showing
1 changed file
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
<script setup lang="ts"> | ||
async function exportCSV() { | ||
await useFetch('/api/csvDownload', { | ||
method: 'get', | ||
onResponse({ response }) { | ||
download(response._data, 'form.csv') | ||
}, | ||
}) | ||
} | ||
function download(data: any, filename: string) { | ||
const defaultMimeType = 'application/octet-stream' | ||
const blob = new Blob([data], { type: defaultMimeType }) | ||
const blobURL = window.URL.createObjectURL(blob) | ||
const tempLink = document.createElement('a') | ||
tempLink.style.display = 'none' | ||
tempLink.href = blobURL | ||
tempLink.setAttribute('download', filename) | ||
if (typeof tempLink.download === 'undefined') | ||
tempLink.setAttribute('target', '_blank') | ||
document.body.appendChild(tempLink) | ||
tempLink.click() | ||
document.body.removeChild(tempLink) | ||
window.URL.revokeObjectURL(blobURL) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<NuxtWelcome /> | ||
<button @click="exportCSV"> | ||
Download CSV | ||
</button> | ||
</div> | ||
</template> |