uploadFile() {
var file = this.$refs.fileData.files[0]
if (file) {
let self = this
if (!/\.edf$/.test(file.name)) {
this.snackbar = true
this.text = '只支持 edf 格式数据!'
return;
}
let fileName = file.name
this.file_name = fileName
var spark = new SparkMD5()
var reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = async function (e) {
spark.appendBinary(e.target.result)
self.hash_value = spark.end()
let bytes = window.atob(this.result.split(',')[1])
let arrayBuffer = new ArrayBuffer(bytes.length)
let intArray = new Uint8Array(arrayBuffer)
for (let i = 0; i < bytes.length; i++) {
intArray[i] = bytes.charCodeAt(i)
}
let blob = new Blob([intArray], {
type: 'application/json'
})
var files = new window.File([blob], fileName, {
type: 'text/plain'
})
let getFileUploadUrlResult = await api.call('BackendServer', 'get_file_upload_url', self.formData.id, fileName, self.hash_value)
self.getFileUploadUrl = getFileUploadUrlResult
if (getFileUploadUrlResult.error === 'missing parameters') {
self.snackbar = true
self.text = '参数错误!'
} else {
if (getFileUploadUrlResult.wave !== '') {
axios.put(getFileUploadUrlResult.wave, files, {
headers: {
'Content-Type': 'multipart/form-data'
},
onUploadProgress: progressEvent => {
self.showProgress = true
self.isDisabled = true
let complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
self.valueDeterminate = complete
if (self.valueDeterminate === 100 + '%') {
self.showProgress = false
self.isDisabled = false
}
}
})
.then(async (response) => {
if (response.status === 200) {
self.snackbar = true
self.text = '上传成功!'
let formData = new FormData()
formData.append('image', files)
let res = await api.call('BackendServer', 'get_upload_url', {'user_id': self.formData.id, 'folder_name': 'brain_wave', 'file_name': fileName })
if (res !==null) {
let fetchResult = await fetch(res.url, {
method: 'POST',
body: formData
})
if (fetchResult.status === 200) {
self.snackbar = true
self.text = '文件保存成功!'
}
}
}
})
}
}
}
}
},
网友评论