element upload完整版代码

<el-upload
								class="upload-demo"
								:action="uploadUrl"
								:data="{ module: 'news-consultation' }"
								ref="upload"
								                        accept=".jpg,.png,.doc,.docx,.ppt,.pdf,.xlsx,.xls,.zip,.rar"
								:auto-upload="false"
								:before-upload="handleBeforeUpload"
								:before-remove="beforeRemove"
								:on-preview="openFile"
								:on-remove="
									(...args) => {
										handelDelFile(...args, 'attacheId')
									}
								"
								:on-change="
									(...args) => {
										handleChangeUpload(
											...args,
											'news-consultation',
											'attacheId'
										)
									}
								"
								multiple
								:file-list="mixFileList"
							>
								<el-button size="small">点击上传</el-button>
								<div slot="tip" class="el-upload__tip">
									支持上传jpg、png、doc、docx、ppt、pdf、xlsx、xls
									、zip、rar,单个文件不得超过40M,最多上传5个文件。
								</div>
							</el-upload>

// 文件上传前的格式校验
		async handleBeforeUpload(file, fileList, type) {
			const nameArray = file.name.split('.')
			const suffix = nameArray[nameArray.length - 1]
			let isSuccess = true
			if (type === 'attacheId') {
				if (!this.fileTypeList.includes(suffix)) {
					this.$message.error(
						'支持上传jpg、png、doc、docx、ppt、pdf、xlsx、xls 、zip、rar文件!'
					)

					isSuccess = false
				}
				const isLtXM = file.size / 1024 / 1024 < 40
				if (!isLtXM) {
					this.$message({
						message: '上传文件大小不能超过 40MB!',
						type: 'warning'
					})
					isSuccess = false
				}
			}
			return isSuccess
		},
		// 文件移除前
		beforeRemove(file, fileList) {
			console.log(file)
			return this.$confirm(`是否删除【${file.name}】?`)
		},
		//下载文件
		openFile(e) {
			console.log(e)
			const link = document.createElement('a')
			link.href = `${window.__CTC_ENV__.apiUrl}/attache/downLoad/${e.id}?response-content-type=application/octet-stream`
			link.target = '_blank'
			link.download = e.name
			link.style.display = 'none'
			document.body.append(link)
			link.click()
		},
		/** 文件移除之后*/
		async handelDelFile(file, fileList, type) {
			console.log(file)
			let res = await AssessmentBase.delattlog({ attid: file.id })
			if (res.code === 200) {
				this.$message({
					type: 'success',
					message: '删除成功'
				})
				this.form.attaid.forEach((i, k) => {
					if (i === file.id) {
						this.form.attaid.splice(k, 1)
					}
				})
				if (type === 'attacheId') {
					this.mixFileList = fileList
				}
			}
		},
		async handleChangeUpload(file, fileList, module, type) {
			console.log(file, type)
			// 导入
			let formData = new FormData() //  用FormData存放上传文件
			formData.append('file', file.raw)
			// formData.append('module', module)

			const isSuccess = await this.handleBeforeUpload(
				file,
				fileList,
				type
			)
			console.log(isSuccess)

			if (isSuccess) {
				AssessmentBase.upload(formData).then(
					res => {
						console.log(res)
						this.form.attaid.push(res.data)
						//手动上传无法触发成功或失败的钩子函数,因此这里手动调用
						this.handleSuccessFile(res.data, file, fileList, type)
					},
					err => {}
				)
			}
		},
		// 文件上传成功
		handleSuccessFile(data, file, fileList, type) {
			if (type === 'attacheId') {
				this.mixFileList.push({
					name: file.name,
					id: data,
					url: `${this.uploadUrl}${data}`
				})
			}
		},

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
element upload是一个基于Vue.js的文件上传组件,可以方便地实现文件上传功能。使用element upload需要先安装element-ui组件库,然后在需要使用的页面中引入相关组件。下面是一个简单的使用element upload的例子: 1. 在HTML文件中引入element-ui组件库和相关CSS文件 ```html <!-- 引入element-ui组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <!-- 引入element-ui组件库的CSS文件 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> ``` 2. 在Vue组件中使用element upload ```html <template> <div> <el-upload class="upload-demo" action="/upload" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :file-list="fileList" :limit="fileLimit" :on-exceed="handleExceed" :headers="headers" :data="{fileType:fileType,fileSize:fileSize}" multiple> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传{{fileType.join(',')}}格式的文件,且不超过{{fileSize}}MB</div> </el-upload> </div> </template> <script> export default { data() { return { fileList: [], // 上传后的文件列表 fileType: ["pdf", "doc", "docx", "xls", "xlsx","txt","png","jpg", "bmp", "jpeg"], // 允许的文件类型 fileSize: 50, // 运行上传文件大小,单位 M fileLimit: 5, // 附件数量限制 headers: { "Content-Type": "multipart/form-data" }, // 请求头 }; }, methods: { handlePreview(file) { console.log(file); }, handleRemove(file, fileList) { console.log(file, fileList); }, beforeUpload(file) { const isLt50M = file.size / 1024 / 1024 < this.fileSize; if (!isLt50M) { this.$message.error(`上传文件大小不能超过 ${this.fileSize}MB!`); } const fileType = file.name.split(".").pop().toLowerCase(); const isFileType = this.fileType.indexOf(fileType) !== -1; if (!isFileType) { this.$message.error(`只能上传${this.fileType.join(',')}格式的文件!`); } return isLt50M && isFileType; }, handleExceed(files, fileList) { this.$message.warning(`当前限制选择 ${this.fileLimit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`); }, }, }; </script> ``` 在上面的代码中,我们使用了el-upload组件来实现文件上传功能。其中,action属性指定了上传文件的地址,on-preview属性指定了文件预览的回调函数,on-remove属性指定了文件移除的回调函数,before-upload属性指定了文件上传前的校验函数,file-list属性指定了上传后的文件列表,limit属性指定了上传文件数量的限制,on-exceed属性指定了上传文件数量超过限制时的回调函数,headers属性指定了请求头,data属性指定了上传文件时需要传递的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值