-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
88 lines (68 loc) · 2.84 KB
/
script.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { HuffmanCoder } from "./huffman.js";
function process(){
console.log("Entered function");
const encode = document.getElementById('encode');//encode button
const decode = document.getElementById('decode');//decode button
const temptext = document.getElementById('temptext');//space for text
const upload = document.getElementById('uploadedFile');//uploaded file
const coder= new HuffmanCoder();
upload.addEventListener('change',()=>{ alert("File uploaded") });//when file is uploaded will display an alert
//clicked on encode
encode.onclick = function(){
const uploadedFile = upload.files[0];
if (uploadedFile===undefined){
alert("No file uploaded!");
return;
}
const fileReader= new FileReader();
fileReader.onload= function(fileLoadedEvent){
const text= fileLoadedEvent.target.result;
if(text.length===0){
alert("Text cannot be empty!");
return;
}
let [encoded,tree_structure,info]=coder.encode(text);
// console.log(encoded);
// console.log(tree_structure)
downloadFile(uploadedFile.name.split('.')[0]+'_encoded.txt',encoded);
temptext.innerText=info;
};
fileReader.readAsText(uploadedFile,"UTF-8");
};
decode.onclick = function () {
const uploadedFile = upload.files[0];
if(uploadedFile===undefined){
alert("No file uploaded !");
return;
}
const fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent){//will see text file now
const text = fileLoadedEvent.target.result;//will get all the written text in file
if(text.length===0){//file is blank
alert("Text can not be empty ! Upload another file !");
return;
}
let [decoded, tree_structure, info] = coder.decode(text);
downloadFile(uploadedFile.name.split('.')[0] +'_decoded.txt', decoded);
temptext.innerText = info;
};
fileReader.readAsText(uploadedFile, "UTF-8");
};
};
function downloadFile(fileName, data){
// let a = document.createElement('a');
// a.href="data:application/octet-stream"+encodeURIComponent(data);
// a.download= fileName;
// a.click();
const blob = new Blob([data], { type: "application/octet-stream" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = fileName;
a.click();
// Clean up the object URL after the download
URL.revokeObjectURL(url);
}
window.onload = function () {
process();
};