Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Commit

Permalink
[refactor] improve file download function
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Mar 20, 2019
1 parent d0b9aa5 commit f399975
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/file/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/**
* Download a file with the the text passed as parameter.
* @param filename
* @param text
* @param content
* @param contentType
*/
function download (filename, text) {
function download (filename, content, contentType = 'text/json') {
const element = document.createElement('a')
const blob = new Blob([content], { type: contentType })

element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text))
element.setAttribute('download', filename)
element.href = window.URL.createObjectURL(blob)
element.download = filename
element.dataset.downloadurl = [contentType, element.download, element.href].join(':')
element.style.display = 'none'

document.body.appendChild(element)
Expand Down

0 comments on commit f399975

Please sign in to comment.