From f3a86d5ba5a57b0ec6ea2dae3073ab30c28d600a Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 17 Dec 2024 15:28:02 -0600 Subject: Add function to read first 'n' lines of file as text. --- uploader/static/js/files.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 uploader/static/js/files.js diff --git a/uploader/static/js/files.js b/uploader/static/js/files.js new file mode 100644 index 0000000..c1dedad --- /dev/null +++ b/uploader/static/js/files.js @@ -0,0 +1,16 @@ +var read_first_n_lines = (fileelement, count, process_content_fns) => { + var thefile = fileelement.files[0]; + var reader = new FileReader(); + if(typeof thefile !== "undefined" && thefile !== null) { + reader.addEventListener("load", (event) => { + var content = event + .target + .result + .split("\n") + .slice(0, count) + .map((line) => {return line.trim("\r");}); + process_content_fns.forEach((fn) => {fn(content);}); + }); + reader.readAsText(thefile); + } +}; -- cgit v1.2.3