element-ui文件上传el-upload

//其中action是上传的地址;
//on-remove是文件列表移除文件时的钩子;
//before-upload是上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传;
//on-success是文件上传成功时的钩子;
//file-list是上传的文件列表
<el-upload
	class="upload-demo"
	ref="upload"
	:action="userImageAction()"
	:on-remove="handleRemove"
	:before-upload="onBeforeUpload"
	:on-success="successFile"
	:with-credentials="true"
	:file-list="fileList"
	list-type="picture">
	<el-button size="small" type="primary">选取图片</el-button>
	<div slot="tip" class="el-upload__tip">支持上传一个jpg/png/jpeg/bmp/gif/webp文件</div>
</el-upload>
export default {
	data(){
		return {
			fileList:[],
			//这是点击添加后要传给后端接口的数据
			productTemp:{
				img:'',
				id:''
			}
		}
	},
	methods:{
		userImageAction() {
			return 'http://127.0.0.1:900/data/fileUpload'
		},
		handleRemove(file,fileList){
			this.fileList=fileList;
		},
		onBeforeUpload(file) {
	      const isImage = file.type === 'image/jpg' || file.type === 'image/png' ||
	          file.type === 'image/jpeg' || file.type === 'image/bmp' ||
	          file.type === 'image/gif' || file.type === 'image/webp'
	      if (!isImage) {
	        this.$message.error('上传图片只能是jpg/png/jpeg/bmp/gif/webp格式!')
	      }
	      const isLt2M = file.size / 1024 / 1024 < 2
	      if (!isLt2M) {
	        this.$message.error('上传头像图片大小不能超过 2MB!')
	      }
	      return isLt2M && isImage
	   },
	   successFile(response, file) {
	      if (response.code === 0) {
	        this.fileList = [file];
	        this.productTemp.img =  'http://127.0.0.1:900/' + response.data;
	      } else {
	        this.$message.error(response.message)
	      }
    	},
	}
}

例子:

<template>
  <div>
    <div>{{productTemp.name}}</div>
    <div>{{productTemp.img}}</div>
    <el-button @click="add">添加</el-button>
    <el-dialog width="35%" :close-on-click-modal="false"
                 :visible.sync="dialogFormVisible">
        <el-form ref="ruleForm" :model="productTemp">
          <el-form-item label="手机名称" prop="name">
            <el-input v-model="productTemp.name" clearable></el-input>
          </el-form-item>
          <el-form-item label="产品图">
            <el-upload
                class="upload-demo"
                ref="upload"
                :action="userImageAction()"
                :on-remove="handleRemove"
                :before-upload="onBeforeUpload"
                :on-success="successFile"
                :with-credentials="true"
                :file-list="fileList"
                list-type="picture">
              <el-button size="small" type="primary">选取图片</el-button>
              <div slot="tip" class="el-upload__tip">支持上传一个jpg/png/jpeg/bmp/gif/webp文件</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="cancel">取 消</el-button>
          <el-button size="small" type="primary" @click="confirmAdd">确 定
          </el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      dialogFormVisible: false,
      fileList:[],
			//这是点击添加后要传给后端接口的数据
			productTemp:{
				img:'',
				name:''
			}
    }
  },
  methods:{
    //点击添加按钮后弹出添加弹框
    add(){
      this.dialogFormVisible=true;
    },
    //点击取消按钮后隐藏弹框
    cancel(){
      this.dialogFormVisible=false;
    },
    //点击确认按钮后是需要将数据传给后端进行添加数据的,但是这里我没有接口所以就没传
    confirmAdd(){
      this.dialogFormVisible=false;
      console.log(this.productTemp);
    },
	userImageAction() {
		return 'http://127.0.0.1:900/data/fileUpload'
	},
	handleRemove(file,fileList){
		this.fileList=fileList;
	},
	onBeforeUpload(file) {
	     const isImage = file.type === 'image/jpg' || file.type === 'image/png' ||
	          file.type === 'image/jpeg' || file.type === 'image/bmp' ||
	          file.type === 'image/gif' || file.type === 'image/webp'
	      if (!isImage) {
	        this.$message.error('上传图片只能是jpg/png/jpeg/bmp/gif/webp格式!')
	      }
	      const isLt2M = file.size / 1024 / 1024 < 2
	      if (!isLt2M) {
	        this.$message.error('上传头像图片大小不能超过 2MB!')
	      }
	      return isLt2M && isImage
	},
	successFile(response, file) {
	      if (response.code === 0) {
	        this.fileList = [file];
	        this.productTemp.img =  'http://127.0.0.1:900/' + response.data;
	      } else {
	        this.$message.error(response.message)
	      }
   		},
	}
}
</script>

<style scoped>

 
</style>

您好,关于 element-ui 的上传组件 el-upload,您可以通过自定义 el-upload-list 来实现自定义上传列表的功能。 具体实现方法如下: 1.el-upload 组件中添加属性 list-type="picture-card",并设置属性 action 为上传文件的地址。 2.el-upload 组件中添加自定义 slot,用于自定义上传列表的显示。 3. 在自定义的上传列表组件中,使用 el-upload-list 组件来展示上传文件的列表。 4. 可以通过监听 el-upload 组件的 change 事件来获取上传的文件信息,并将上传的文件信息传递给自定义的上传列表组件,以展示上传文件的列表。 以下是一个简单的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/upload" list-type="picture-card" :on-change="handleChange" > <i class="el-icon-plus"></i> <div class="upload-text">上传图片</div> </el-upload> <custom-upload-list :file-list="fileList"></custom-upload-list> </div> </template> <script> import CustomUploadList from './CustomUploadList.vue'; export default { components: { CustomUploadList, }, data() { return { fileList: [], }; }, methods: { handleChange(file, fileList) { this.fileList = fileList; }, }, }; </script> ``` CustomUploadList.vue 组件的代码如下: ```html <template> <el-upload-list class="custom-upload-list" :file-list="fileList"> <template slot-scope="{file}"> <div class="custom-list-item"> <img :src="file.url" class="custom-list-item-thumbnail"> <div class="custom-list-item-name">{{ file.name }}</div> <div class="custom-list-item-actions"> <el-button size="small" type="text" @click="handleRemove(file)">删除</el-button> </div> </div> </template> </el-upload-list> </template> <script> export default { props: { fileList: { type: Array, default: () => [], }, }, methods: { handleRemove(file) { const index = this.fileList.indexOf(file); if (index !== -1) { this.fileList.splice(index, 1); } }, }, }; </script> ``` 希望这个示例能够帮助到您。如果您有任何问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值