cmc-web/packages/common/utils/uploadFile.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-08-20 12:11:31 +00:00
// websocket文件上传
import { getToken } from './auth'
2024-08-21 01:17:14 +00:00
export default function uploadFile(item, callback, errorCallBack) {
2024-08-20 12:11:31 +00:00
const protocol = location.protocol === 'http:' ? 'ws' : 'wss'
2024-08-21 01:17:14 +00:00
item.isReady = true
const socket = new WebSocket(`${protocol}://${location.host}/api/sms/uploadService`, getToken())
let i = 0
let startSize = 0,
endSize = 0
const paragraph = 4 * 1024 * 1024 // 以4MB为一个分片
const count = parseInt(item.file.size / paragraph) + 1
2024-08-20 12:11:31 +00:00
socket.onopen = function () {
2024-08-21 01:17:14 +00:00
item.isUploading = true
socket.send(
JSON.stringify({
filename: item.file.name,
upload: 'file'
})
)
2024-08-20 12:11:31 +00:00
// 取消上传
item.cancel = function () {
2024-08-21 01:17:14 +00:00
item.progress = 0
socket.send(
JSON.stringify({
UPLOAD_CANCEL: 'UPLOAD_CANCEL'
})
)
item.isUploading = false
}
}
2024-08-20 12:11:31 +00:00
socket.onmessage = function (event) {
const sendFile = function () {
if (startSize < item.file.size) {
2024-08-21 01:17:14 +00:00
let blob
endSize += paragraph
2024-08-20 12:11:31 +00:00
if (item.file.webkitSlice) {
2024-08-21 01:17:14 +00:00
blob = item.file.webkitSlice(startSize, endSize)
2024-08-20 12:11:31 +00:00
} else if (item.file.mozSlice) {
2024-08-21 01:17:14 +00:00
blob = item.file.mozSlice(startSize, endSize)
2024-08-20 12:11:31 +00:00
} else {
2024-08-21 01:17:14 +00:00
blob = item.file.slice(startSize, endSize)
2024-08-20 12:11:31 +00:00
}
2024-08-21 01:17:14 +00:00
const reader = new FileReader()
reader.readAsArrayBuffer(blob)
2024-08-20 12:11:31 +00:00
2024-08-21 01:17:14 +00:00
reader.onload = function loaded(evt) {
const result = evt.target.result
2024-08-20 12:11:31 +00:00
// i++;
// const isok = (i / count) * 100;
// item.progress = parseInt(isok);
2024-08-21 01:17:14 +00:00
startSize = endSize
socket.send(result)
}
2024-08-20 12:11:31 +00:00
} else {
2024-08-21 01:17:14 +00:00
item.progress = 100
socket.send(
JSON.stringify({
sendover: 'sendover'
})
)
2024-08-20 12:11:31 +00:00
}
2024-08-21 01:17:14 +00:00
}
item.isUploading = true
item.isCancel = false
const obj = JSON.parse(event.data)
2024-08-20 12:11:31 +00:00
if (obj.category == 'UPLOAD_ACK') {
2024-08-21 01:17:14 +00:00
item.filePath = obj.content
sendFile()
2024-08-20 12:11:31 +00:00
} else if (obj.category == 'UPLOAD') {
if (obj.content == 'SAVE_FAILURE') {
2024-08-21 01:17:14 +00:00
item.isUploading = false
errorCallBack(item)
2024-08-20 12:11:31 +00:00
} else if (obj.content == 'SAVE_SUCCESS') {
2024-08-21 01:17:14 +00:00
sendFile()
i++
const isok = (i / count) * 100
item.progress = parseInt(isok)
2024-08-20 12:11:31 +00:00
} else if (obj.content == 'TRUE') {
2024-08-21 01:17:14 +00:00
callback(item)
item.isReady = true
item.isSuccess = true
item.isUploading = false
socket.close()
2024-08-20 12:11:31 +00:00
}
} else if (obj.category == 'UPLOAD_CANCEL') {
2024-08-21 01:17:14 +00:00
item.progress = 0
item.isCancel = true
socket.close()
2024-08-20 12:11:31 +00:00
}
2024-08-21 01:17:14 +00:00
}
2024-08-20 12:11:31 +00:00
}