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

Downloading .ics file #197

Closed
tjin88 opened this issue Aug 19, 2021 · 2 comments
Closed

Downloading .ics file #197

tjin88 opened this issue Aug 19, 2021 · 2 comments

Comments

@tjin88
Copy link

tjin88 commented Aug 19, 2021

How would you download a .ics file? Is there a built-in method I can use, or should I use the buildEvent method to return a string, then convert to .ics myself?

@mijkal
Copy link

mijkal commented Aug 19, 2021

If you're using something like Express with a REST endpoint, your final response will be something like this:

res
.set(
'Content-Disposition',
'attachment; filename="${FILE_NAME}.ics"',
)
.set('Content-Type', 'text/calendar')
.status(200)
.send(ics);
}

They key is to set your HTTP headers so clients know how to handle the response (in this case, an ics text file).

The ics is either generated in a function prior to the final response code above, or perhaps loaded from a cached source (in the case of a feed you probably don't want to regenerate it on every request, so you have other mechanisms to update the feed snapshot).

@Chris112
Copy link

For the use case where you're generating client side, you can use the popular library downloadjs or create a simple utility function.

  function downloadFile(config: { fileContents: any; fileName: string; mimeType: string }) {
    const { mimeType, fileContents, fileName } = config
    const blob = new Blob([fileContents], { type: mimeType })
    const url = URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.setAttribute('href', url)
    link.setAttribute('download', fileName)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
  
  // ...
downloadFile({
    mimeType: 'text/calendar',
    fileContents: value,
    fileName: 'meeting.ics',
})

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

No branches or pull requests

4 participants