This commit is contained in:
2025-09-12 10:38:17 +08:00
parent 914c04c240
commit a1e14096b9
2 changed files with 74 additions and 3 deletions

View File

@@ -125,7 +125,11 @@
:data="upload.data"
:on-progress="handleFileUploadProgress"
:on-success="handleFileSuccess"
:on-error="handleFileError"
:on-exceed="handleFileExceed"
:auto-upload="false"
:with-credentials="false"
:http-request="customHttpRequest"
drag
>
<i class="el-icon-upload"></i>
@@ -465,6 +469,51 @@ export default {
this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
this.getList();
},
// 文件上传失败处理(网络/后端错误)
handleFileError(err, file, fileList) {
this.upload.isUploading = false;
this.$message.error('上传失败:' + (err && (err.msg || err.message) || '网络错误'));
},
// 文件数超出限制
handleFileExceed(files, fileList) {
this.$message.warning('一次最多上传 1 个文件');
},
// 自定义上传请求:支持大文件进度与中断恢复(预留)
async customHttpRequest(options) {
const { file, onProgress, onSuccess, onError } = options
// 直接透传给默认 xhr但保留钩子可用于分片
try {
const form = new FormData()
form.append('file', file)
Object.keys(this.upload.data || {}).forEach(k => form.append(k, this.upload.data[k]))
const xhr = new XMLHttpRequest()
xhr.open('POST', this.upload.url, true)
const headers = this.upload.headers || {}
Object.keys(headers).forEach(k => xhr.setRequestHeader(k, headers[k]))
xhr.upload.onprogress = function (evt) {
if (evt.lengthComputable && typeof onProgress === 'function') {
onProgress({ percent: Math.round((evt.loaded / evt.total) * 100) })
}
}
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const res = JSON.parse(xhr.responseText || '{}')
onSuccess && onSuccess(res)
} catch (e) {
onSuccess && onSuccess({ msg: '上传成功' })
}
} else {
onError && onError(new Error('HTTP ' + xhr.status))
}
}
}
xhr.send(form)
} catch (e) {
onError && onError(e)
}
},
// 提交上传文件
submitFileForm() {
// 将选择的元数据同步到上传表单字段