<el-upload
class="upload-demo"
:action="uploadFileUrl"
:on-success="handleSuccess"
:file-list="fileList"
:auto-upload="true"
:limit="1"
:on-change="beforeUpload"
:data="{'xiangmuhao': scope.row.xiangmuhao}"
:headers="uploadHeaders"
>
<template #trigger>
<el-button type="primary">请选择上传资料</el-button>
</template>
</el-upload>
const uploadFileUrl = ref(import.meta.env.VITE_APP_BASE_API + "/hsmanuscript/qyzd/upload"); // 上传文件服务器地址
const uploadHeaders = ref({
'Authorization': 'Bearer ' + Cookies.get('Admin-Token') // 让每个请求携带自定义token 请根据实际情况自行修改
});
/**文件上传成功的回调 */
const handleSuccess = (response, file, fileList) => {
// 文件上传成功的回调,你可以在这里处理服务器返回的数据
proxy.$alert(response.msg);
uploadFlag.value = true;
fileList = [];
file = null;
getList();
console.log(response);
};
/**上传文件之前的钩子,你可以在这里进行文件类型的检查、大小限制等*/
const beforeUpload = (file) => {
myFile.value = file;
//console.log(file.raw.type)
// xlsx=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
// xls=application/vnd.ms-excel
// jpg=image/jpeg
// png=image/png
// doc=application/msword
// docs=application/vnd.openxmlformats-officedocument.wordprocessingml.document
// pdf=application/pdf
// 上传文件之前的钩子,你可以在这里进行文件类型的检查、大小限制等
/*const isJPG = file.type === 'image/jpeg';
const isPNG = file.type === 'image/png';
const isLt500k = file.size / 1024 < 500;
if (!isJPG && !isPNG) {
proxy.$message.error('上传图片只能是 JPG/PNG 格式!');
}*/
return true//isJPG || isPNG
/*if (!isLt500k) {
this.$message.error('上传图片大小不能超过 500KB!');
}
return isJPG || isPNG && isLt500k;*/
};
const submitForm2 = (xiangmuhao) => {
console.log("file", myFile.value)
console.log("xiangmuhao", xiangmuhao)
const formData = new FormData();
formData.append('file', myFile.value.raw);
formData.append('xiangmuhao', xiangmuhao);
uploadFile(formData).then(
response => {
proxy.$alert(response.msg);
uploadFlag.value = true;
fileList.value = [];
getList();
}
).catch(e => {
alert("文件上传失败");
});
// 提交表单的函数,你可以在这里发送请求到服务器,同时包含用户名和文件信息
// 这里只是一个示例,你需要根据自己的需求进行实现
// 你可以通过 axios 或其他 HTTP 客户端库发送请求到服务器
// 在请求体中,你可以包含 form.value.username 和 fileList 的信息
};