------【Vue】|Vue 中实现 excel文件上传功能

【------【Vue】|Vue 中实现 excel文件上传功能】场景: 上传excel表,并将excel表中的数据构建成实体

上传excel

beforeUpload(file){ // console.log(file,'文件'); this.files = file; const extension = file.name.split('.')[1] === 'xls' const extension2 = file.name.split('.')[1] === 'xlsx' const isLt5M = file.size / 1024 / 1024 < 5 if (!extension && !extension2) { this.$message.warning('上传模板只能是 xls、xlsx格式!') return } if (!isLt5M) { this.$message.warning('上传模板大小不能超过 5MB!') return } this.fileName = file.name; setTimeout(() => { this.submitUpload(); },500); return false; // 返回false不会自动上传 },// 上传excel submitUpload() { console.log('上传'+this.files.name) if(this.fileName == ""){ this.$message.warning('请选择要上传的文件!') return false } let fileFormData = https://www.it610.com/article/new FormData(); fileFormData.append("code", "t_pathology_info_excel"); fileFormData.append("description", "excel上传测试"); //filename是键,file是值,就是要传的文件,test是要传的文件名 fileFormData.append('files', this.files, this.fileName); let requestConfig = { headers: { 'Content-Type': 'multipart/form-data' }, } // 执行上传excel let id = ''; this.http.postJson('/sys/file/upload', fileFormData, requestConfig).then(resp => { if (resp.data.status != 200) { this.$message.error("excel上传失败,请重新上传"); } else { id = resp.data.result[0].attachmentId; } }).catch((e) => { // console.log(e); this.$message.error("excel上传失败,请重新上传"); }).finally(() => { if(id) { // 触发生成订单实体数据 this.generateBill(id); } }); }// 构建实体 generateBill(attachmentId) { this.http.postJson('/admin/myTest/leadingIn/' + attachmentId + '/type_one').then(resp => { if (resp.data.status != 200) { // this.$message.error("excel上传失败,请重新上传"); } else { this.$message.success("excel上传成功!"); this.query(); } }).catch((e) => { // console.log(e); // this.$message.error("excel上传失败,请重新上传"); }).finally(() => {}); }


在这里遇到了死锁:Lock wait timeout exceeded; try restarting transaction
我在将excel表中的数据构建成实体的方法中加入了:@Transactional(rollbackOn = RuntimeException.class) 注解
手动抛异常throw new RuntimeException(); 正常应该是出现异常自动回滚,但是这里出现是事务锁,将数据库的附件表锁死了
猜想原因:一开始上传的附件,没有绑定关联id,而是选择了在构建实体方法中:先绑定关联,然后根据关联
取出excel表。由于上传附件的方法是异步执行的,报了异常事务回滚,出现事务锁将数据库的附件表锁死了。
解决办法:
1,数据库对应的表需要解锁,解决:https://blog.csdn.net/weixin_41888813/article/details/86659433
2,在上传附件的方法中,同时将关联id绑定
/** * 文件上传&绑定relIdexcel上传诊单数据 * * @param userId当前登录人Id * @param username当前登陆用户名称 * @param code上传编码标识code * @param des描述 * @param files附件 */ @PostMapping(value = "https://www.it610.com/sys/file/upload4excelImport") public Message upload4excelImport(@CurrentUser Long userId, @CurrentUser String username, @CurrentUser("relId") Long relId, @RequestParam(name = "code") String code, @RequestParam(name = "des") String des, @RequestParam MultipartFile[] files) {// 第一步,generateAttachment本地生成附件方法// 第二步,绑定附件表关联idreturn Message.success(); }


    推荐阅读