feat: Batch upload

This commit is contained in:
zhouwei 2021-02-05 16:25:33 +08:00
parent d64262676a
commit 282c19dcf7

View File

@ -1,5 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh"> <html lang="zh">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
@ -7,13 +8,14 @@
</head> </head>
<body> <body>
<input id="file" type="file" accept="audio/mpeg" /> <input id="file" type="file" multiple/>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"></script>
<script> <script>
const phone = '' // 这里填手机号
const phone = '' const password = '' // 这里填密码
const password = '' const fileUpdateTime = {}
let fileLength = 0
let cookieToken = '' let cookieToken = ''
if (!phone || !password) { if (!phone || !password) {
const msg = '请设置你的手机号码和密码' const msg = '请设置你的手机号码和密码'
@ -24,32 +26,53 @@
main() main()
async function login() { async function login() {
const res = await axios({ const res = await axios({
url: `/login/cellphone?phone=${phone}&password=${password}`, url: `/login/cellphone?phone=${phone}&password=${encodeURIComponent(password)}`,
withCredentials: true, //关键 withCredentials: true, //关键
}) })
cookieToken = res.data.cookie cookieToken = res.data.cookie
} }
async function main() { function main() {
document document
.querySelector('input[type="file"]') .querySelector('input[type="file"]')
.addEventListener('change', function (e) { .addEventListener('change', function (e) {
var file = this.files[0] console.log(this.files)
upload(file) let currentIndx = 0
fileLength = this.files.length
for (const item of this.files) {
currentIndx += 1
upload(item, currentIndx)
}
}) })
} }
async function upload(file) { function upload(file, currentIndx) {
var formData = new FormData() var formData = new FormData()
formData.append('songFile', file) formData.append('songFile', file)
const res = await axios({ axios({
method: 'post', method: 'post',
url: `http://localhost:3000/cloud?time=${Date.now()}`, url: `http://localhost:3000/cloud?time=${Date.now()}`,
headers: { headers: {
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
}, },
data: formData, data: formData,
}).then(res => {
console.log(`${file.name} 上传成功`)
if (currentIndx >= fileLength) { console.log('上传完毕') }
}).catch(async err => {
console.log(err)
console.log(fileUpdateTime)
fileUpdateTime[file.name] ? fileUpdateTime[file.name] += 1 : fileUpdateTime[file.name] = 1
if (fileUpdateTime[file.name] >= 4) {
console.error(`丢,这首歌怎么都传不上:${file.name}`)
return
} else {
console.error(`${file.name} 失败 ${fileUpdateTime[file.name]} 次`)
}
await login()
upload(file, currentIndx)
}) })
} }
</script> </script>
</body> </body>
</html> </html>