-
Notifications
You must be signed in to change notification settings - Fork 0
/
try.js
executable file
·69 lines (61 loc) · 1.82 KB
/
try.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
var Key = generateRandomKey();
$( function() {
$('#upload').on('click', function(){
var fileInput = document.getElementById('fileInput').files[0];
var reader = new FileReader();
reader.onload = function() {
var uploadData = reader.result;
var pos1 = uploadData.indexOf(':');
var pos2 = uploadData.indexOf(';');
var pos3 = uploadData.indexOf(',');
var FileName = fileInput.name;
var MIMEType = uploadData.substr(pos1+1,pos2-pos1-1);
var FileData = uploadData.substr(pos3+1);
// encryption
FileData = encryptMessage(FileData, Key);
$.post('save_file.php',
{FileName: FileName, MIMEType: MIMEType, FileData: FileData},
function(data) {
if(data=='1') console.log('Successfully saved on the server.');
else console.log('Error: ' + data);
}
);
}
reader.readAsDataURL(fileInput);
});
$('#download').on('click', function() {
$.post('get_file.php', {}, function(data) {
if(data=='0') console.log('Error: ' + data);
else {
// decrypt
data = decryptMessage(data, Key);
if(bowser.safari) {
// anything but IE
var a = document.createElement('a');
a.setAttribute('download', 'test.pdf');
a.href = 'data:application/pdf;base64,'+data;
a.innerHTML = 'testing';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
}
else {
// anything but Safari
var byteArray = base64_to_byte_array(data);
var blob = new Blob([byteArray], {type: 'application/pdf'});
var sss = saveAs(blob, 'test.pdf');
}
}
});
});
});
function base64_to_byte_array(data) {
data = atob(data);
var dataLength = data.length;
var byteNumbers = new Array(dataLength);
for(var i = 0; i < dataLength; i++) {
byteNumbers[i] = data.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
return byteArray;
}