element-ui upload

自定义Vue上传组件
本文介绍了一种基于Element-UI的自定义Vue上传组件实现,该组件支持图片预览及格式大小验证等功能,并详细展示了如何将文件上传功能整合到产品添加流程中。

之前项目里面需要添加产品,除了添加产品名称等字段外,还要上传产品图片,然后看了element ui 的框架,觉得不太适合,于是自己在element-ui基础上改写了一个组件,见代码

这个是upload.vue组件
<template> <el-upload class="avatar-uploader" :disabled="useDisabled" action="" :auto-upload="false" :on-change="submitFile" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="previewUrl" :src="previewUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </template> <script> import { Upload } from 'element-ui' export default { props: ['imageUrl', 'useDisabled', 'name'], data() { return { url: undefined, disabled: false } }, components: { "el-upload": Upload }, computed: { previewUrl() { if (this.url) { return this.url; } let reg = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}(=)?|[A-Za-z0-9+/]{2}(==)?|[A-Za-z0-9+/]{1}(===)?)$/ if (reg.test(this.imageUrl)) { if (this.imageUrl.indexOf('data:image', 0) == -1) { return `data:image/jpeg;base64,${this.imageUrl}`; } } //这里的功能主要是修改时候,获取单条产品信息会返回图片字段,这里就不用加上图片base64解析 data:image/jpeg;base64 return this.imageUrl } }, methods: { preview(file) { //这里是预览图片 var fr = new FileReader() fr.onloadend = () => { this.url = fr.result; } fr.readAsDataURL(file.raw) }, submitFile(file) { // var formData = new FormData(); //调用接口上传data:formData // formData.append('file', file.raw); //这里要说明一下,如果直接使用element-ui的upload组件的时候,这里的formData字段就是拿到的文件,可以直接传给后台,但是我要把这个上传功能放在页面去处理,于是向上触发了 this.preview(file); this.$emit('uploadFun', { name: this.name, file }) // console.log(formData) }, handleAvatarSuccess(res, file) { this.url = URL.createObjectURL(file.raw); }, beforeAvatarUpload(file) { const isJPG = file.type === 'image/jpeg'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('上传头像图片只能是 JPG 格式!'); } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!'); } return isJPG && isLt2M; } } } </script> <style> </style>


引入upload组件后,在addProduct.vue页面中调用

<upload-img v-on:uploadFun="uploadFun" name="productImage" :imageUrl="productImage"> </upload-img>  //这里的productImage就是后台需要的字段
  uploadFun(file) {
  this.files[file.name] = file.file.raw;
  },//upload组件中派发的方法
 

然后在点击添加产品按钮上绑定函数 

         let proInfo = {
            product:product
            }
            var formData = new FormData()
            for (let key in this.files) {
                formData.append(key, this.files[key])
            }
            for (let key in proInfo) {
                formData.append(key, proInfo[key])
            }    

然后传给后台 formData字段即可。

转载于:https://www.cnblogs.com/alhh/p/9111227.html

Element-UIUpload组件主要用于处理文件上传,包括图片、文本等常见格式。如果你想要手动上传Excel文件,你可以按照以下步骤操作: 1. 首先,你需要安装Element-UI以及相关的文件上传插件,如`axios`用于发送HTTP请求,因为`element-upload`本身并不直接支持上传Excel文件。 ```bash npm install element-ui axios ``` 2. 在Vue项目中引入Element-UIUpload组件: ```html <template> <el-upload :action="uploadUrl" :on-change="handleChange" :file-list="fileList" accept=".xls,.xlsx" <!-- 表示接受.xls和.xlsx类型的文件 --> > <i class="el-icon-upload"></i> 点击<em>上传</em> </el-upload> </template> <script> import axios from 'axios'; export default { components: { ElUpload }, data() { return { fileList: [], uploadUrl: 'your-api-url-to-upload', // 替换为你实际的上传接口地址 }; }, methods: { handleChange(file) { if (!file.type.includes('application/vnd.ms-excel')) { this.$message.error('只支持Excel文件'); return; } const reader = new FileReader(); reader.onload = (e) => { axios.post(this.uploadUrl, e.target.result, { headers: { 'Content-Type': 'multipart/form-data' } }) .then(response => { console.log('上传成功:', response); // 更新文件列表或处理服务器响应 }) .catch(error => { console.error('上传失败:', error); }); }; reader.readAsArrayBuffer(file); } } }; </script> ``` 在这个例子中,当用户选择一个Excel文件后,`handleChange`方法会读取文件内容,并使用axios发送POST请求到指定的API。注意,由于浏览器安全限制,`FileReader` API只能读取二进制数据,所以我们需要设置`Content-Type`为`multipart/form-data`以便于服务器识别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值