原生上传文件(DOM+button)

69 阅读1分钟

这里选择原生上传文件,主要的用处在于,可以自定义上传,对于element-plus里面的el-upload有很多的上传语法的限制,这里用原生的上传方法要好的很多

<el-button size="small" style="margin-right: 10px;" @click="handleExceed" type="primary">导入提货数据</el-button>
                       const handleExceed = async()=>{
				let input = document.getElementById('inputFile') as HTMLInputElement;
				if(!input){
					input = document.createElement('input');
					input.type = 'file';
					input.onchange = (e:any) => {
						uploadExcel(e);
					};
					input.click();
				}
			};
			const uploadExcel=async (e:any)=> {	
				const type = ['application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'].includes(e.target.files[0].type);
				if(!type) {
					ElMessage.error('只接受xls、xlsx格式的文件');
					e.target.value = '';
					return false;
				}
				let formData = new FormData(); //这一步不能变
				formData.append('file',e.target.files[0])
				const res = await service.upload(formData);
				if(res.code != "000000"){
					
				}else{
					ElMessage.success('上传成功');
					getList();
				};
      };