-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
61 lines (57 loc) · 1.92 KB
/
index.html
File metadata and controls
61 lines (57 loc) · 1.92 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input id="ipt-file" type="file">
<script>
document.getElementById('ipt-file').addEventListener('change', async function() {
let file = this.files[0];
this.value = '';
if(file.size > (7 << 20)){
// Lambda limits 10MB, refer: https://stackoverflow.com/a/37553294/6414094
// Base64 size is 4/3 of blob, so it safer to limit 7MB.
let errorMsg = 'The file is too large, please limit it to 7M or less.';
alert(errorMsg)
throw Error(errorMsg);
// If you want to take full advantage of this 10MB size,
// use the binary processing capabilities of API Gateway.
// Or use AWS S3 which has more storage capacity.
}
let base64 = await new Promise(rs=>{
let fileReader = new FileReader();
fileReader.onload = ()=>{ rs(fileReader.result) }
fileReader.readAsDataURL(file);
});
base64 = base64.substring(base64.indexOf(',') + 1);
let rep;
try{
// AWS Console -> Lambda -> Function -> Your function name ->
// Configuartion -> Function URL -> Copy
rep = await fetch('https://<url-id>.lambda-url.<region>.on.aws/', {
method: 'POST',
body: base64,
});
}catch(ex){
alert('Network error!');
throw ex;
}
if(!rep.ok){
let txt = await rep.text();
alert(txt);
throw Error(txt);
}
let results = await rep.json();
console.log(results);
let arrTxt = [];
for(let i of results){
arrTxt.push(i.text);
}
let strTxts = arrTxt.join(',\n');
alert(strTxts);
});
</script>
</body>
</html>