element-ui中upload组件获取上传文件信息

本文详细介绍了Element UI中上传组件的高级使用技巧,包括如何自定义上传请求方式,如使用POST或PATCH请求,以及如何实现手动上传,限制文件类型和大小,获取上传文件信息并进行统一上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用htttp-request自定义上传

在这里插入图片描述
使用element-ui中的上传组件,upload,这个默认发送post请求,
在使用upload组件自动携带的请求方式发送
action->请求的url
on-success->请求发送成功的钩子–方法function(response, file, fileList)默认参数3个
auto-upload->是否在选取文件后立即进行上传,默认是true(选取文件即上传)
name->上传的文件字段名,默认值file,后台有时候需要发送别的字段名如image就这修改
data->上传时附带的额外参数object
disabled->是否禁用,默认false
http-request->覆盖默认的上传行为,可以自定义上传的实现–方法(当请求方式不为post可以自定义方式)

下面是两个例子
1.使用upload默认请求方式post发送提交图片请求,后台要求文件字段image
在这里插入图片描述
在这里插入图片描述
在文件上传成功后,可以从response的data属性获取图片在服务器上的路径,文件的具体信息在file.raw中
在这里插入图片描述
2.使用upload上传文件,发送patch请求数据,需要使用http-request钩子
由于覆盖了其默认请求,updateImg中异步请求携带了路径,action中就随便放了个网址,action是必传参数
在这里插入图片描述
http-reques钩子有个默认参数,val.file可以获取文件数据信息
在这里插入图片描述
在这里插入图片描述

2.使用手动上传

需求背景: 有个form表单, 通过上传组件获取上传文件信息, 点击弹窗的确定按钮后,接口统一进行上传;
限制上传zip类型文件,不超过10M,限制选择1个zip包
在这里插入图片描述
在这里插入图片描述

<!-- 文件上传-->
<el-form :rules="rules" :model="dataForm" ref="dataForm" label-width="150px" @submit.native.prevent>
      <el-form-item label="名称:" prop="name">
        <el-input type="text" v-model.trim="dataForm.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="文件:" prop="file" >
        <el-upload
            class="upload-import"
            ref="uploadImport"
            action="https://baidu.com/" <!-- 手动上传, action随便给了个地址-->
            :on-remove="handleRemove"
            :file-list="fileList"
            :limit="1" <!-- 限制上传数量-->
            :on-change="handleChange"
            :auto-upload="false"	<!-- 关闭自动上传-->
            accept="application/zip,.zip">  <!-- 设置接收的文件类型-->
            <!-- el-upload组件,在手动上传时,禁用按钮外, 还需要设置    :disabled="hasFile"   为true禁用该组件,会导致上传列表也被禁用,无法删除,因此使用v-show,文件数量为1时,显示禁用的的按钮, slot不绑定trigger事件 --> 
          <el-button v-show="!hasFile" slot="trigger" size="small" type="primary" >选取文件</el-button>
          <el-button v-show="hasFile"  size="small" type="primary" disabled>选取文件</el-button>
          <div slot="tip" class="el-upload__tip">只能上传zip文件,且不超过10M</div>
        </el-upload>
      </el-form-item>
 </el-form>

<script>
export default {
	data(){
		return {
			dataForm: {
	          name: '',
	          file: null
	        },
	        rules: {
	          name: [
	            {required: true, message: "请输入名称", trigger: "blur"},
	            {max: 50, message: "最长可输入50个字符", trigger: "blur"},
	            {validator: isvalidatName, trigger: "blur" },
	          ],
	          file: [
	            {required: true, message: "请选择上传文件", trigger: "blur"},
	          ]
        },
        hasFile: false,
        fileList: []
	},
	methods: {
		 handleRemove(file, fileList) {
	        if (!fileList.length) {
	          this.hasFile = false;
	        }
	        this.dataForm.file = null;
	      },
	      handleChange(file, fileList) {
    	    if (fileList.length >= 2) {
	          return;
	        }
	        if (fileList.length === 1) {
	          this.hasFile = true;
	        }
	        this.dataForm.file = file;
	      },
	      //确定按钮
	      onSureHandle(){
				this.$refs.dataForm.validate(valid => {
					if(valid){
						let dataPar = this.dataForm;
						
						let fdParams = new FormData();
						fdParams.append('name', dataPar.name);
						fdParams.append('file', dataPar.file.raw);

					axios.post('/interface/importProject', fdParams, {
							headers: {'Content-Type': 'multipart/form-data'},//定义内容格式,很重要
							timeout: 20000,
						}).then(response => {
							//...some logic
						}).catch(err =>{})						
					}
				})
		  }
	}
}
</script>
      ```

要使用 element-uiupload 组件回显图片,可以将图片地址存储在一个数组中,然后在 upload 组件的 `success` 回调函数中将图片地址添加到这个数组中。然后,可以通过 v-for 指令将这个存储图片地址的数组渲染成一个图片列表。 以下是一个示例代码: ``` <template> <div> <el-upload class="avatar-uploader" action="/upload" :show-file-list="false" :on-success="handleSuccess" > <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> <div v-if="imageList.length > 0"> <div v-for="(image, index) in imageList" :key="index"> <img :src="image" class="uploaded-image"> </div> </div> </div> </template> <script> export default { data() { return { imageUrl: '', imageList: [] } }, methods: { handleSuccess(response, file) { this.imageUrl = URL.createObjectURL(file.raw) this.imageList.push(this.imageUrl) } } } </script> <style scoped> .avatar-uploader { display: inline-block; width: 100px; height: 100px; border-radius: 50%; border: 1px dashed #ccc; text-align: center; background-size: cover; background-position: center; overflow: hidden; } .avatar { width: 100%; height: 100%; display: block; } .avatar-uploader-icon { font-size: 28px; color: #999; line-height: 100px; } .uploaded-image { width: 150px; height: 150px; margin-right: 10px; margin-bottom: 10px; } </style> ``` 在这个示例中,我们使用了 element-uiupload 组件上传图片,并且在上传成功后将图片地址存储在 `imageList` 数组中。然后,我们使用 v-for 指令将这个数组中的图片地址渲染成一个图片列表。如果需要修改已上传的图片,可以在图片列表中添加一个按钮,点击后弹出一个上传组件上传新的图片并更新数组中对应的图片地址即可。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值