-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFileHandler.js
More file actions
54 lines (49 loc) · 1.77 KB
/
TextFileHandler.js
File metadata and controls
54 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @class TextFileHandler
* @classdesc Utility class for downloading and uploading plain text files in the browser.
* @example
* // Download example
* TextFileHandler.download("Hello world!", "hello.txt");
*
* // Upload example
* document.querySelector("#textInput").addEventListener("change", async (e) => {
* const file = e.target.files[0];
* const text = await TextFileHandler.upload(file);
* console.log(text);
* });
*/
export class TextFileHandler {
/**
* Downloads a string as a plain text file.
* @param {string} text - The text content to save.
* @param {string} [filename='data.txt'] - The filename for the downloaded file.
* @throws {TypeError} If text is not a string.
* @returns {void}
*/
static download(text, filename = 'data.txt') {
if (typeof text !== 'string') {
throw new TypeError('Expected a string for the text parameter.');
}
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
/**
* Reads the contents of a plain text file selected by the user.
* @param {File} file - The text file to read, typically from an <input type="file"> element.
* @returns {Promise<string>} Resolves with the text content of the file.
* @throws {TypeError} If the input is not a File object.
*/
static async upload(file) {
if (!(file instanceof File)) {
throw new TypeError('Expected a File object.');
}
return file.text();
}
}