-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.js
126 lines (100 loc) · 3.43 KB
/
ajax.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const ws = new WebSocket("ws://localhost:8989");
let downloadname
ws.onopen = () => {
ws.send("hello server")
}
ws.onclose = () => {
console.log('服务器断开了连接')
}
ws.onmessage = (message) => {
console.log('收到服务器消息')
const BlobReader = new FileReader()
let data = message.data
if (isJSON(data)) {
document.getElementById('FileList').innerHTML = ""
document.getElementById('message').innerHTML = ""
data = JSON.parse(data)
if (data instanceof Array) {
for (let n = 0; n < data.length; n++) {
let list = document.createElement("input")
const br = document.createElement("br")
list.setAttribute('type', 'radio')
list.setAttribute('value', data[n])
list.setAttribute('name', 'option')
document.getElementById('FileList').append(list)
document.getElementById('FileList').append(data[n])
document.getElementById('FileList').append(br)
}
let button = document.createElement('button')
button.setAttribute('onclick', 'download()')
button.innerText = '接收'
document.getElementById('FileList').append(button)
}
}
if (isJSON(data) == false && data == '连接成功') {
document.getElementById('message').append(data)
}
if (data instanceof Blob) {
const cv = document.createElement('canvas')
const img = new Image();
const reader = new FileReader();
let src
blobToDataURL(data, (url) => {
src = url.replace('application/octet-stream', 'png')
img.src = src
img.onload = function () {
if (this.width && this.height) {
const ctx = cv.getContext('2d');
cv.setAttribute('width', this.width)
cv.setAttribute('height', this.height)
ctx.drawImage(this, 0, 0, this.width, this.height, 0, 0, this.width, this.height); //this即是imgObj,保持图片的原始大小:470*480
//ctx.drawImage(this, 0, 0,1024,768);//改变图片的大小到1024*768
document.getElementById('PictureShow').append(cv)
/*cv.toBlob(function (blob) {
saveAs(blob, downloadname);
});*/
}
}
})
console.log('下载启动')
console.log(data)
var file = new File([data], downloadname); //浏览器原生解码不支持中文
saveAs(file);
}
}
ws.onerror = (err) => {
console.log(err)
}
function getFileList() {
ws.send('getFile')
}
function download() {
var radio = document.getElementsByName("option");
for (i = 0; i < radio.length; i++) {
if (radio[i].checked) {
downloadname = radio[i].value
ws.send(radio[i].value)
}
}
}
function isJSON(str) {
if (typeof str == 'string') {
try {
var obj = JSON.parse(str);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
}
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function (e) {
callback(e.target.result);
}
a.readAsDataURL(blob);
}