elementui php上传图片,vue中如何使用element-ui的Upload上传组件

本篇文章主要介绍了在vue项目中使用element-ui的Upload上传组件的示例,现在分享给大家,也给大家做个参考。

本文介绍了vue项目中使用element-ui的Upload上传组件的示例,分享给大家,具体如下:

v-else

class='ensure ensureButt'

:action="importFileUrl"

:data="upLoadData"

name="importfile"

:onError="uploadError"

:onSuccess="uploadSuccess"

:beforeUpload="beforeAvatarUpload"

>

确定

其中importFileUrl是后台接口,upLoadData是上传文件时要上传的额外参数,uploadError是上传文件失败时的回掉函数,uploadSuccess是文件上传成功时的回掉函数,beforeAvatarUpload是在上传文件之前调用的函数,我们可以在这里进行文件类型的判断。data () {

importFileUrl: 'http:dtc.com/cpy/add',

upLoadData: {

cpyId: '123456',

occurTime: '2017-08'

}

},

methods: {

// 上传成功后的回调

uploadSuccess (response, file, fileList) {

console.log('上传文件', response)

},

// 上传错误

uploadError (response, file, fileList) {

console.log('上传失败,请重试!')

},

// 上传前对文件的大小的判断

beforeAvatarUpload (file) {

const extension = file.name.split('.')[1] === 'xls'

const extension2 = file.name.split('.')[1] === 'xlsx'

const extension3 = file.name.split('.')[1] === 'doc'

const extension4 = file.name.split('.')[1] === 'docx'

const isLt2M = file.size / 1024 / 1024 < 10

if (!extension && !extension2 && !extension3 && !extension4) {

console.log('上传模板只能是 xls、xlsx、doc、docx 格式!')

}

if (!isLt2M) {

console.log('上传模板大小不能超过 10MB!')

}

return extension || extension2 || extension3 || extension4 && isLt2M

}

}

最近在适用VUE作为前端框架做自己的项目,在做到需要上传文件到服务器时,同事告诉我upload之中的action也就是上传地址不能动态的去更改,然后去看了一下,需要做以下处理才能动态的使用:

action是一个必填参数,且其类型为string,我们把action写成:action,然后后面跟着一个方法名,调用方法,返回你想要的地址,代码示例://html 代码

点击上传

// js 代码在 methods中写入需要调用的方法

methods:{

UploadUrl:function(){

return "返回需要上传的地址";

}

}

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

1. 首先,在项目安装element-ui和axios: ``` npm install element-ui axios --save ``` 2. 在main.js引入element-ui和axios: ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import axios from 'axios' Vue.use(ElementUI) Vue.prototype.$axios = axios ``` 3. 在组件使用上传组件: ``` <template> <div> <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleSuccess" :before-upload="beforeUpload" > <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> ``` 4. 在组件定义上传前和上传成功的方法: ``` <script> export default { data() { return { imageUrl: '' } }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg' const isPNG = file.type === 'image/png' if (!isJPG && !isPNG) { this.$message.error('只能上传jpg或png格式的图片') return false } const isLt2M = file.size / 1024 / 1024 < 2 if (!isLt2M) { this.$message.error('上传的图片大小不能超过2MB') return false } return true }, handleSuccess(response) { this.imageUrl = response.data.url } } } </script> ``` 5. 在服务器端,需要接收上传的图片,并将其保存到指定路径: ``` const express = require('express') const multer = require('multer') const path = require('path') const app = express() const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, path.join(__dirname, '/public/images')) }, filename: function (req, file, cb) { const extname = path.extname(file.originalname) cb(null, Date.now() + extname) } }) const upload = multer({ storage: storage }) app.post('/api/upload', upload.single('file'), (req, res) => { const url = `http://localhost:3000/images/${req.file.filename}` res.json({ code: 0, data: { url: url } }) }) app.listen(3000, () => { console.log('server is running at http://localhost:3000') }) ``` 以上就是在Vue使用element-ui上传组件上传图片的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值