aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-12-17 15:28:02 -0600
committerFrederick Muriuki Muriithi2024-12-17 15:59:06 -0600
commitf3a86d5ba5a57b0ec6ea2dae3073ab30c28d600a (patch)
tree1813e5d942a5d8dabcfd5ad9d32ef92a373ebc44
parent13f4a952a4a97223a8a0319c8bf92b5b0feeae57 (diff)
downloadgn-uploader-f3a86d5ba5a57b0ec6ea2dae3073ab30c28d600a.tar.gz
Add function to read first 'n' lines of file as text.
-rw-r--r--uploader/static/js/files.js16
1 files changed, 16 insertions, 0 deletions
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);
+ }
+};