国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ant-design-vue中upload上傳圖片、視頻實現(xiàn)預覽功能

這篇具有很好參考價值的文章主要介紹了ant-design-vue中upload上傳圖片、視頻實現(xiàn)預覽功能。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

有沒有小伙伴在使用ant-design-vue的upload組件時,發(fā)現(xiàn)api文檔在圖片預覽功能的介紹寥寥無幾,而且也沒提供視頻預覽的demo,在實際開發(fā)中碰到相應的需求直撓頭~~~~,別急,下面來給大家分享一個我自己封裝的upload組件,符合需求可以直接在項目中放到組件目錄調用。

template部分代碼:

?

<template>
  <div>
    <a-upload
      name="file"
      :multiple="true"
      :action="uploadAction"
      list-type="picture-card"
      :headers="headers"
      :data="{'isup':1,'bizPath':bizPath}"
      :fileList="fileList"
      :beforeUpload="beforeUpload"
      @preview="handlePreview"
      @change="handleChange"
      :disabled="disabled">
        <div v-if="fileList.length < 8">
          <a-icon type="plus" />
          <div class="ant-upload-text">
            上傳
          </div>
        </div>
    </a-upload>
    <a-modal v-if="fileType == 'image'" :visible="previewVisible" width="1400px" :footer="null" @cancel="handleImgCancel">
      <img  alt="example" style="width: 100%" :src="previewSrc" />
    </a-modal>
    <a-modal v-if="fileType == 'video/mp4'" :visible="previewVisible" :footer="null" @cancel="handleVideoCancel">     
      <video alt="example" style="width: 100%" :src="previewSrc" controls>
      </video>
    </a-modal>
  </div>
</template>

script部分代碼:

<script>
  import Vue from 'vue'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  const FILE_TYPE_ALL = "all"
  const FILE_TYPE_IMG = "image"
  const FILE_TYPE_MP4 = "video/mp4"
  const FILE_TYPE_TXT = "file"
  const uidGenerator=()=>{
    return '-'+parseInt(Math.random()*10000+1,10);
  }
  const getFileName=(path)=>{
    if(path.lastIndexOf("\\")>=0){
      let reg=new RegExp("\\\\","g");
      path = path.replace(reg,"/");
    }
    return path.substring(path.lastIndexOf("/")+1);
  }
  export default {
    name: 'JUpload',
    data(){
      return {
        uploadAction:window._CONFIG['domianURL']+"/sys/common/upload",//圖片上傳地址
        imgErver: window._CONFIG['domianURL'] + '/sys/common/view',//圖片預覽地址
        videoErver: window._CONFIG['domianURL'] + 'sys/common/view_video',//視頻預覽地址
        isCreatFile:true,//是否創(chuàng)建文件占位dom
        headers:{},
        fileList: [],
        previewSrc:'',
        previewVisible:false,
      }
    },
    props:{
      text:{
        type:String,
        required:false,
        default:"點擊上傳"
      },
      fileType:{
        type:String,
        required:false,
        default:FILE_TYPE_ALL
      },
      /*這個屬性用于控制文件上傳的業(yè)務路徑*/
      bizPath:{
        type:String,
        required:false,
        default:"temp"
      },
      value:{
        type:[String,Array],
        required:false
      },
      // update-begin- --- author:wangshuai ------ date:20190929 ---- for:Jupload組件增加是否能夠點擊
      disabled:{
        type:Boolean,
        required:false,
        default: false
      },
    },
    watch:{
      value(val){
        console.log(val)
        if (val instanceof Array) {
          this.initFileList(val.join(','))
        } else {
          this.initFileList(val)
        }
      }
    },
    created(){
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },

    methods:{
      initFileList(paths){
        if(!paths || paths.length==0){
          //return [];
          // update-begin- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload組件初始化bug
          this.fileList = [];
          return;
          // update-end- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload組件初始化bug
        }
        let fileList = [];
        let arr = paths.split(",")
        for(var a=0;a<arr.length;a++){
          fileList.push({
            uid:uidGenerator(),
            name:getFileName(arr[a]),
            status: 'done',
            url: this.urlDownload+arr[a],
            response:{
              status:"history",
              message:arr[a]
            }
          })
        }
        this.fileList = fileList
      },
      handlePathChange(){
        let uploadFiles = this.fileList
        let path = ''
        if(!uploadFiles || uploadFiles.length==0){
          path = ''
        }
        let arr = [];

        for(var a=0;a<uploadFiles.length;a++){
          if(uploadFiles[a].response){
            arr.push(uploadFiles[a].response.message)
          }      
        }
        if(arr.length>0){
          path = arr.join(",")
        }
        this.$emit('change', path);
      },
      beforeUpload(file){//上傳文件前對文件類型判斷
        var fileType = file.type;
        if(this.fileType === FILE_TYPE_ALL){        
          this.isCreatFile = true;
          return true;
        }
        else if(fileType === this.fileType){
          this.isCreatFile = true;
          return true;
        }
        else{
          if(this.fileType === FILE_TYPE_IMG){
            if(fileType.indexOf('image')<0){
              this.$message.warning('請上傳圖片');
              this.isCreatFile = false;
              return false;
            }
          }else if(this.fileType === FILE_TYPE_TXT){
            if(fileType.indexOf('image')>=0){
              this.$message.warning('請上傳文件');
              this.isCreatFile = false;
              return false;
            }
          }
          else if(this.fileType === FILE_TYPE_MP4){
            if(fileType.indexOf('mp4')<0){
              this.$message.warning('請上傳mp4文件');
              this.isCreatFile = false;
              return false;
            }
          }
        }
        //TODO 擴展功能驗證文件大小
      },
      handleChange(info) {
        if(this.isCreatFile == false){
          return false
        }
        let fileList = info.fileList
        if(info.file.status==='done'){
          if(info.file.response.success){
            fileList = fileList.map((file) => {
              if (file.response) {
                file.url = this.urlDownload+file.response.message;
              }
              return file;
            });
          }
          this.$message.success(`${info.file.name} 上傳成功!`);
        }else if (info.file.status === 'error') {
          this.$message.error(`${info.file.name} 上傳失敗.`);
        }else if(info.file.status === 'removed'){
          this.handleDelete(info.file)
        }
        this.fileList = fileList
        if(info.file.status==='done' || info.file.status === 'removed'){
          this.handlePathChange()
        }
      },
      handleImgCancel() {
        this.previewVisible = false;
      },
      handleVideoCancel(){
        this.previewVisible = false;
        let myVideo = document.getElementsByTagName('video');
        let arr = Array.from(myVideo)
        arr.forEach(item =>{//關閉時暫停播放
          item.pause()
        })
      },
      async handlePreview(file) {//判斷打開不同文件預覽窗口
        if(this.fileType == 'image'){
          if (!file.url && !file.preview) {
            file.preview = await getBase64(file.originFileObj);
          }
          this.previewSrc = file.url || file.preview;
          this.previewVisible = true;
        }
        if(this.fileType == 'video/mp4'){
          this.previewSrc = this.imgErver + '/' + file.response.message;
          this.previewVisible = true;
        }
      },
      handleDelete(file){
        //如有需要新增 刪除邏輯
        console.log(file)
      },
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>

好人做到底,組件調用也貼上:

<JUpload @change="saveImageUrl" ref="imgUpload" :fileType="'image'" :value="savedImageList"></JUpload>

最后要注意的地方:

antd 視頻上傳vue,組件封裝,anti-design-vue,前端

?如果需要多次上傳,記得在重置組件文件列表,不然會有緩存文章來源地址http://www.zghlxwxcb.cn/news/detail-523204.html

到了這里,關于ant-design-vue中upload上傳圖片、視頻實現(xiàn)預覽功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 關于 ant-design-vue resetFields 失效

    關于 ant-design-vue resetFields 失效

    關于 ant-design-vue resetFields 失效 背景: 遇到這樣的問題使用 ant-design-vue useForm 來制作表單的時候, resetFields()失效 場景: 編輯 -賦值 新增 -初始值(問題點:新增的時候他就不 初始化 ) 方案: 1、調用 resetFields() 傳參 2、要么使用 reactive 明確定義初始值 解釋: 首先我這里講

    2024年01月17日
    瀏覽(23)
  • Ant Design upload 文件上傳 限制文件只能上傳一個

    Ant Design upload 文件上傳 限制文件只能上傳一個

    上傳前: ? ? ? ? ? ? ? ? 回顯:可以刪除? ?最近做了一個后臺管理系統(tǒng)使用的是 Ant Design和vue框架搭建的 文件上傳 :組件:? Ant Design? ?https://1x.antdv.com/components/upload-cn/? (upload 官方文檔) 功能需求 : 1.可以拖拽,或者點擊上傳文件? 2.只能上傳單個文件,不能上傳多個文

    2024年02月14日
    瀏覽(25)
  • ant-design-vue 自由切換 暗黑模式dark

    ant-design-vue 自由切換 暗黑模式dark

    思路 引入 dark.css 文件 動態(tài)切換 prefixCls 實現(xiàn)效果 我們來看看官網(wǎng)怎么說的 官網(wǎng)地址 除了 less 定制主題 外,我們還提供了 CSS Variable 版本以支持動態(tài)切換主題能力。你可以在 ConfigProvider 進行體驗。 調用 ConfigProvider 配置方法設置主題色: 默認情況下,CSS Variable 會以 --ant 作

    2023年04月21日
    瀏覽(21)
  • 國際化配置(ant-design-vue設置成中文)

    國際化配置(ant-design-vue設置成中文)

    vue3 + ts + ant-design-vue 引用ant-design-vue的組件時,默認是英文的。 官網(wǎng)說明:https://www.antdv.com/components/date-picker-cn ? 我用的全局設置。在項目的App.vue文件里引入ant-design-vue自帶的zh_CN,用a-config-provider 將RouterView包裹起來。但是我發(fā)現(xiàn)設置了全局,在使用日期選擇框時還是英文,

    2024年02月22日
    瀏覽(17)
  • ant-design-vue表格Table行內(nèi)新增、編輯、刪除

    ant-design-vue表格Table行內(nèi)新增、編輯、刪除

    ant-design-vue表格Table進行單元格內(nèi)新增、編輯、刪除等操作 如圖所示:

    2024年02月14日
    瀏覽(22)
  • ant-design-vue 4.x升級問題-樣式丟失問題

    該文檔是在升級ant-design-vue到4.x版本的時候遇到的問題 以上是開發(fā)項目時使用的包以及包的版本,使用的腳手架是vite 當使用ant-design-vue3.x版本時沒有任何問題,但是當升級ant-design-vue到4.0版本時,因為ant-design-vue4.x使用了css-in-js需要修改vite.config.js配置 變更為 修改后當開發(fā)環(huán)

    2024年02月14日
    瀏覽(82)
  • Ant-design-vue AutoComplete 自動補全組件的使用

    AutoComplete 是一個輸入框自動完成功能,在輸入時提個建議或者輔助提示。 和 Select 的區(qū)別是: AutoComplete 是一個帶提示的文本輸入框,用戶可以自由輸入,是輔助輸入 Select 是在限定的可選項中進行選擇,是選擇。 使用案例如下: filterOption 是否根據(jù)輸入項進行

    2024年02月12日
    瀏覽(23)
  • [ant-design-vue] table組件列寬拖拽功能

    原有的樣式文件沒有使用的必要,個人添加的部分樣式內(nèi)容就符合需求了 vue3.x對應的ant-design-vue中的table組件本身支持列寬的拖動了,不需求額外的包的支持,根據(jù)Api說明設置resizeColumn即可

    2024年01月23日
    瀏覽(28)
  • vue2安裝ant-design UI報錯 ERR! peer vue@“>=3.2.0“ from ant-design-vue@3.2.15

    vue2安裝ant-design UI報錯 ERR! peer vue@“>=3.2.0“ from ant-design-vue@3.2.15

    npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: default@0.1.0 npm ERR! Found: vue@2.7.14 npm ERR! ? vue@\\\"^2.6.10\\\" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer vue@\\\"=3.2.0\\\" from ant-design-vue@3.2.15 npm ERR! node_modules/ant-design-vue npm ERR! ? ant-design

    2024年02月01日
    瀏覽(26)
  • ant-design-vue的table表格行合并和列合并

    ant-design-vue的table表格行合并和列合并

    場景說明: 1、列合并:需要在表格最后一列進行合并,如圖: 思路:相當于是第二列的最后一欄(colSpan )占比5列,第2列后面的4列最后一行(colSpan )占比0。 代碼示例 行合并需求如圖:將指定列的多行合并成一行 思路:和合并列差不多;第一列的第一行和第二行要合并

    2024年02月16日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包