vue中使用elementUI组件的Upload 上传图片

1. 安装elementUI:npm i element-ui -S
2. 在main.js中引入elementUI
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(Element, { size: 'small' })
3. 访问Element官网【组件–Upload上传】

在这里插入图片描述

<el-upload
  action="https://jsonplaceholder.typicode.com/posts/"
  list-type="picture-card"
  :on-preview="handlePictureCardPreview"
  :on-remove="handleRemove">
  <i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
  <img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
<script>
  export default {
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      }
    }
  }
</script>
4. 我的需求:点击保存按钮手动上传,只能上传一张图片,参数为userId

在这里插入图片描述

5. script中的data( )和methods( )如下:
  export default {
    data() {
      return {
      	file:'',
        dialogImageUrl: '',
        dialogVisible: false,
        imgUrl:'',
        userId: '',
        uid:'',
        
        // 上传图片时附带的额外参数userId
        resData:{
        	userId: window.localStorage["userId"]
        },
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      
      // 点击保存按钮上传图片
      submit2:function(res){
		this.$refs.upload.submit();
	},
		
	// 图片上传成功后,后台返回图片的路径
	onSuccess:function(res){
		// console.log(res);
		if(res.status==200){
			this.imgUrl=res.data.imgUrl;
		}
	},
    }
  }
6. 该组件的效果图如下:

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在Vue2使用ElementUI2上传视频,你需要完成以下步骤: 1. 安装ElementUI2和Vue2 ```bash npm install element-ui@2.15.1 npm install vue@2.6.14 ``` 2. 引入ElementUI2和样式 在你的Vue项目入口文件,引入ElementUI2和它的CSS文件。 ```js import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建上传组件 在你的Vue组件使用ElementUI2的`el-upload`组件创建一个上传组件。你需要设置`action`属性为上传视频的URL,`on-success`属性为上传成功后的回调函数。 ```html <template> <el-upload class="upload-demo" action="/uploadVideo" :on-success="handleUploadSuccess" :on-error="handleUploadError" :before-upload="beforeUpload" :auto-upload="false" :file-list="fileList"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="handleUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传mp4文件</div> </el-upload> </template> <script> export default { data() { return { fileList: [] } }, methods: { handleUpload() { this.$refs.upload.submit() }, handleUploadSuccess(response, file, fileList) { console.log(response) }, handleUploadError(error, file, fileList) { console.log(error) }, beforeUpload(file) { const isMp4 = file.type === 'video/mp4' if (!isMp4) { this.$message.error('只能上传mp4文件') } return isMp4 } } } </script> ``` 4. 处理上传视频的后端逻辑 在你的后端服务器上,你需要编写接收上传视频的接口,并将视频保存到服务器上。你可以使用Node.js和Express框架来编写接口,使用`multer`间件来处理文件上传。 ```js const express = require('express') const multer = require('multer') const app = express() const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uploads/') }, filename: function (req, file, cb) { cb(null, Date.now() + '-' + file.originalname) } }) const upload = multer({ storage: storage }) app.post('/uploadVideo', upload.single('video'), (req, res) => { const file = req.file if (!file) { const error = new Error('Please upload a file') error.httpStatusCode = 400 return res.send(error) } res.send(file) }) app.listen(3000, () => { console.log('Server started on port 3000') }) ``` 这样,你就可以使用ElementUI2在Vue2上传视频了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

归途风景111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值