export-form.js
2 months ago
file.js
2 months ago
import-form.js
2 months ago
import.js
2 months ago
index.js
2 months ago
file.js
42 lines
| 1 | /** |
| 2 | * Downloads a file. |
| 3 | * |
| 4 | * @param {string} fileName File Name. |
| 5 | * @param {string} content File Content. |
| 6 | * @param {string} contentType File mime type. |
| 7 | */ |
| 8 | export function download(fileName, content, contentType) { |
| 9 | const file = new window.Blob([content], { type: contentType }); |
| 10 | |
| 11 | // IE11 can't use the click to download technique |
| 12 | // we use a specific IE11 technique instead. |
| 13 | if (window.navigator.msSaveOrOpenBlob) { |
| 14 | window.navigator.msSaveOrOpenBlob(file, fileName); |
| 15 | } else { |
| 16 | const a = document.createElement('a'); |
| 17 | a.href = URL.createObjectURL(file); |
| 18 | a.download = fileName; |
| 19 | |
| 20 | a.style.display = 'none'; |
| 21 | document.body.appendChild(a); |
| 22 | a.click(); |
| 23 | document.body.removeChild(a); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Reads the textual content of the given file. |
| 29 | * |
| 30 | * @param {File} file File. |
| 31 | * @return {Promise<string>} Content of the file. |
| 32 | */ |
| 33 | export function readTextFile(file) { |
| 34 | const reader = new window.FileReader(); |
| 35 | return new Promise((resolve) => { |
| 36 | reader.onload = () => { |
| 37 | resolve(reader.result); |
| 38 | }; |
| 39 | reader.readAsText(file); |
| 40 | }); |
| 41 | } |
| 42 |