没有合适的资源?快使用搜索试试~ 我知道了~
elementui上传图片
资源推荐
资源详情
资源评论
在 Element UI 中,可以使用 el-upload 组件来实现图片上传功能。
首先,需要引入 el-upload 组件和 axios 库(用于上传图片):
```html
<template>
<el-upload
class="upload-demo"
action="https://www.example.com/upload"
:headers="{ Authorization: 'Bearer ' + token }"
:before-upload="beforeUpload"
:on-success="onSuccess"
:on-error="onError"
:auto-upload="false"
:list-type="'picture-card'"
:file-list="fileList"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
token: '', // 授权 Token
fileList: [], // 文件列表
}
},
methods: {
beforeUpload(file) {
// 限制图片大小
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 2MB!')
return false
}
// 设置文件名为当前时间戳
const timestamp = Date.now()
file.name = `${timestamp}.${file.name.split('.').pop()}`
return true
资源评论
梦想是坚持
- 粉丝: 69
- 资源: 233
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功