vue项目:element-ui---el-upload上传视频

第一种: 


          <!-- 上传 -->
          <el-upload v-if="ruleForm.jump === 1" class="upload-demo text-images" action="https://up.qbox.me" :limit="1"
            :data="formData" list-type="picture-card" :file-list="photo" :before-upload="beforeUpload"
            :on-preview="handlePictureCardPreview" :on-success="handleSuccess" :before-remove="beforeRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <!-- <img width="100%" :src="dialogImageUrl" alt=""> -->
            <video :src="dialogImageUrl" controls="controls" width="320" height="240" style="margin-left: 10px;"></video>
          </el-dialog>
        

<script>
  import $ from "jquery";
  export default {
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        formData: {
          token: '',
          key: ""
        },
        photo: [],
        ruleForm: {
          jump: 1,
          content: '',
          win_img: '',
        },
      };
    },
    methods: {

      //获取token
      async getToken() {
        let res = await this.$api.qiniu.getToken();
        if (res.data.success) {
          this.formData.token = res.data.token;
        }
      },
      S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      },
      guid() {
        return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this
          .S4() + this.S4());
      },
      //上传文件之前
      beforeUpload(file) {
        const isJPG = ["image/jpeg", "image/jpg", "image/png", "video/mp4"].indexOf(file.type) > -1;
        if (!isJPG) {
          return this.$message.warning('请上传JPG、PNG格式的图片 !');
        }
        //每次上传文件之前设置key
        this.getToken();
        this.formData.key = this.ruleForm.win_img = new Date().getTime().toString() + this.guid();
      },
      //图片预览
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      //文件上传成功
      handleSuccess(response, file, fileList) {
        this.$nextTick(() => {
          $(".text-images .el-upload--picture-card").hide();
          $(".text-images .el-upload-list .el-upload-list__item").prepend('<video></video>');
          $(".text-images .el-upload-list .el-upload-list__item video").addClass("el-upload-list__item-thumbnail");
          $(".text-images .el-upload-list .el-upload-list__item video").attr({
            "width": "320px",
            "height": "240px",
            "controls":"controls",
            "src":file.url
          })
          $(".text-images .el-upload-list .el-upload-list__item img").remove();
        })
      },
      //删除文件
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}?`, {
          type: "warning"
        }).then(() => {
          this.ruleForm.win_img = "";
          $(".text-images .el-upload--picture-card").show();
        });
      },
    },
    mounted() {
      this.getToken();
    }
  }
</script>

<style lang="less" scoped>
  .text-images {
    margin: 15px 0;

    /deep/ .el-upload--picture-card {
      width: 150px;
      height: 150px;
    }

    /deep/ .el-upload-list__item {
      width: 150px;
      height: 150px;
    }
  }

  /deep/ .el-form-item__label {
    font-weight: 400;
    font-size: 14px;
  }

  /deep/ .el-dialog {
    width: 25%;
  }

  .transfer-dialog /deep/ .el-dialog {
    width: 50%;
  }

  .err-tips {
    font-size: 12px;
    line-height: 20px;
  }
</style>
<style type="text/css">
  .el-message-box .el-message-box__content .el-message-box__message {
    padding: 0;
    margin: 60px 0 6px;
  }
</style>

 优化:

<!-- 上传图片或视频 -->
              <el-form-item label="xxx">
                <el-popover placement="right-start" title width="280" trigger="hover" top-content>
                  <img style="width: 250px;" src="https://cdn.lljgame.com/bc12_online/mall_main.png" alt="" />
                  <div class="bc_icon_instruction" slot="reference" style="margin-left: -5px;"></div>
                </el-popover>
                <el-upload
                  class="upload-demo upload-images"
                  action="https://up.qbox.me"
                  :limit="1"
                  :data="formData"
                  list-type="picture-card"
                  :file-list="fileList"
                  :before-upload="beforeUpload"
                  :on-preview="handlePictureCardPreview"
                  :on-success="handleSuccess"
                  :before-remove="beforeRemove"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                  <div style="text-align: center;">
                    <img v-if="is_images" width="100%" :src="dialogImageUrl" alt="" />
                    <video v-else :src="dialogImageUrl" controls="controls" width="320" height="240" style="margin-left: 10px;"></video>
                  </div>
                </el-dialog>
              </el-form-item>




data() {
    return {
      is_images: false,
      dialogImageUrl: '',
      dialogVisible: false,
      formData: {
        token: '',
        key: ''
      },
      fileList: [],
      ruleForm: {
        win_img: ''
      },
    };
  },


//上传文件
    beforeUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 10; //大小不能超过10MB
      let isJPG = ['image/jpeg', 'image/jpg', 'image/png', 'video/mp4', 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb'].includes(
        file.type
      );
      if (!isJPG) {
        return this.$message.warning('请上传 jpeg、jpg、png、mp4、ogg、flv、avi、wmv、rmvb 格式的图片或视频 !');
      }
      this.getToken(); // 每次上传文件之前设置key
      this.formData.key = this.ruleForm.win_img = this.$utils.randomWord(false, 50);
    },
    //上传成功
    handleSuccess(response, file, fileList) {
      const isJPG = ['image/jpeg', 'image/jpg', 'image/png'];
      this.is_images = isJPG.includes(file.raw.type);
      if (isJPG.includes(file.raw.type)) {
        $('.upload-images .el-upload--picture-card').hide();
      } else {
        this.$nextTick(() => {
          $('.upload-images .el-upload--picture-card').hide();
          $('.upload-images .el-upload-list .el-upload-list__item').prepend('<video></video>');
          $('.upload-images .el-upload-list .el-upload-list__item video').addClass('el-upload-list__item-thumbnail');
          $('.upload-images .el-upload-list .el-upload-list__item video').attr({
            width: '320px',
            height: '240px',
            controls: 'controls',
            src: file.url
          });
          $('.upload-images .el-upload-list .el-upload-list__item img').remove();
        });
      }
    },
    //图片预览
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //删除文件
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`, {
        type: 'warning'
      }).then(() => {
        this.ruleForm.win_img = '';
        $('.upload-images .el-upload--picture-card').show();
      });
    },








  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在Vue中使用Element UI上传图片并预览可以按照以下步骤进行: 1. 首先,在项目中安装Element UI和axios: ``` npm install element-ui axios --save ``` 2. 在main.js中引入Element UI和axios,并使用: ```javascript 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. 在组件中使用`<el-upload>`组件进行文件上传,并使用`<el-image>`组件进行预览: ```html <template> <div> <el-upload action="/api/upload" :on-success="handleSuccess" :on-preview="handlePreview" list-type="picture-card"> <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="dialogVisible"> <el-image :src="imageUrl" :preview-src-list="previewImages"></el-image> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, imageUrl: '', previewImages: [] }; }, methods: { handleSuccess(response) { if (response.status === 200) { this.imageUrl = response.data.url; this.previewImages.push(response.data.url); } }, handlePreview(file) { this.dialogVisible = true; this.imageUrl = file.url; this.previewImages = [file.url]; } } } </script> ``` 4. 在服务器端设置相应的文件上传接口,例如使用Node.js和express: ```javascript const express = require('express'); const multer = require('multer'); const app = express(); const upload = multer({ dest: 'uploads/' }); app.post('/api/upload', upload.single('file'), (req, res) => { // 处理上传文件并返回文件URL const file = req.file; const url = `http://example.com/${file.filename}`; res.send({ url }); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` 上述代码实现了一个简单的上传图片并预览的功能,具体可根据实际需求进行调整和扩展。 ### 回答2: 在Vue中使用Element UI上传图片并预览可以按照以下步骤进行: 1. 首先,在项目中引入Element UI组件库,并在需要使用的组件中进行注册。 ```js import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 2. 创建一个上传组件,并初始化需要的数据,如图片列表和上传接口等。 ```vue <template> <div> <el-upload action="/your-upload-url" :on-success="handleSuccess" :file-list="fileList" :on-remove="handleRemove" multiple > <el-button size="small" type="primary"> 选择图片 </el-button> </el-upload> <el-image v-for="(file, index) in fileList" :key="index" :src="file.url" :preview-src-list="previewList" fit="cover" style="width: 100px; height: 100px;" ></el-image> </div> </template> <script> export default { data() { return { fileList: [], // 上传的文件列表 previewList: [] // 预览图片列表 } }, methods: { handleSuccess(response, file, fileList) { // 上传成功的回调函数 fileList[fileList.length - 1].url = URL.createObjectURL(file.raw) this.previewList = fileList.map(file => file.url) }, handleRemove(file, fileList) { // 上传文件移除的回调函数 this.previewList = fileList.map(file => file.url) } } } </script> ``` 在上述代码中,el-upload组件负责上传图片,action属性指定上传图片的接口地址,on-success属性可以监听上传成功的事件,并通过handleSuccess方法返回上传成功后的处理方式。file-list属性用于展示上传成功的文件列表,并且绑定了on-remove属性来处理上传文件的移除事件。 el-image组件用于展示预览图片,v-for指令遍历渲染fileList数组中的每张图片,通过src属性绑定图片的地址,在handleSuccess方法中将上传成功的文件通过URL.createObjectURL方法生成临时的URL作为图片的地址。同时,preview-src-list属性绑定了previewList数组,用于展示预览图片的列表。 这样,当用户选择图片并上传成功后,页面将会展示上传的图片,并提供预览功能。 ### 回答3: 使用Element UI库实现图片上传并预览的步骤如下: 1. 首先,在项目中引入Element UIVue插件。 ``` import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 2. 在Vue组件中,添加一个上传组件和一个用于预览图片的容器。 ``` <template> <div> <!-- 图片上传组件 --> <el-upload class="upload-demo" action="/your-upload-api" <!-- 上传图片的接口地址 --> :on-success="handleUploadSuccess" > <el-button size="small" type="primary">点击上传</el-button> </el-upload> <!-- 图片预览容器 --> <div class="preview-container"> <img :src="imageUrl" v-if="imageUrl" alt="预览图片" /> </div> </div> </template> <script> export default { data() { return { imageUrl: '' // 预览图片的URL }; }, methods: { handleUploadSuccess(response) { // 上传成功后,将返回的图片URL赋值给imageUrl this.imageUrl = response.data.imageUrl; } } }; </script> ``` 3. 完成上述代码后,当用户选择图片并上传成功时,上传接口会返回图片的URL。通过`handleUploadSuccess`方法将返回的URL赋值给`imageUrl`,然后在预览容器中显示预览图片即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前端报刊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值