Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat(examples): add CSV export functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Dec 13, 2023
1 parent 0973d61 commit e786d18
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion examples/p-json2csv/app.vue
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>

0 comments on commit e786d18

Please sign in to comment.