index.html
3.63 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style>
.wrapper {
margin: 0 auto;
max-width: 1024px;
}
.result {
min-width: 300px;
margin-top: 20px;
border: 1px solid #f6efef;
border-radius: 10px;
padding: 10px;
box-shadow: -1px 0px 1px 1px #f6efef;
height: 500px;
overflow: auto;
}
.hidden {
display: none;
}
.show {
display: block;
}
.trans-btn {
margin-top: 20px;
}
.loading {
margin-top: 20px;
font-weight: 600;
}
</style>
</head>
<body>
<div class="wrapper">
<h1>音频转文字</h1>
<p>有两种方式(只可选择其中一种,如若都选中,以上传的内容为准),然后点击“转文字”按钮</p>
<p>1. 上传文件</p>
<div>
<input id="fileInput" type="file" />
</div>
<p>2. 输入URL</p>
<div>
<input id="fileInput2" type="text">
</div>
<button type="button" onclick="clearInput()">清除</button>
<button class="trans-btn" id="uploadButton">转文字</button>
<div id="loading" class="hidden loading">正在转化中,请稍等...</div>
<div id="result" class="result hidden"></div>
</div>
<script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
// 获取上传控件
const upload = document.getElementById('uploadButton')
const result = document.getElementById('result')
const loadingDom = document.getElementById('loading')
let inputContent = ''
upload.addEventListener('click', function() {
result.classList.add('hidden')
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const fileInput2 = document.getElementById('fileInput2');
inputContent = fileInput2.value
if (!file && !inputContent) {
alert('请选择一个文件或者输入URL');
return;
}
loadingDom.classList.remove('hidden')
loadingDom.classList.add('show')
if (file) {
const formData = new FormData();
formData.append('file', file);
getURL(formData)
} else {
getText(inputContent)
}
})
const getText = async (inputContent) => {
const response = await axios.post('/fileTrans', {fileLink: inputContent}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const data = response.data;
console.log(data)
loadingDom.classList.remove('show')
loadingDom.classList.add('hidden')
let _result = ''
if (data.statusCode) {
alert(`${data.statusText}转换失败,错误码:${data.statusCode}`)
} else {
_result = data.map(item => item.Text).join(' ')
result.innerHTML = _result
result.classList.remove('hidden')
}
}
// 获取URL并调用转文字接口
const getURL = async(formData) => {
const response = await axios.post('/upload', formData);
getText(response.data)
}
function clearInput() {
document.getElementById('fileInput').value = '';
document.getElementById('fileInput2').value = '';
result.classList.add('hidden')
}
</script>
</body>
</html>