最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vant實(shí)現(xiàn)上傳多個(gè)圖片或視頻,更改視頻預(yù)覽圖

 更新時(shí)間:2022年10月20日 12:00:16   作者:Magic0901  
這篇文章主要介紹了Vant實(shí)現(xiàn)上傳多個(gè)圖片或視頻,更改視頻預(yù)覽圖,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vant上傳多個(gè)圖片或視頻,更改視頻預(yù)覽圖

需求

  • vant上傳多個(gè)視頻或圖片
  • 圖片和視頻都有預(yù)覽圖

最終成果

過程

  • 最開始是準(zhǔn)備通過自定義預(yù)覽樣式,通過 preview-cover 插槽可以自定義覆蓋在預(yù)覽區(qū)域上方的內(nèi)容。但問題是會修改每一個(gè)上傳的圖片和視頻,都添加上播放視頻的圖片,不能實(shí)現(xiàn)直接預(yù)覽圖片的效果
  • 最終采用自定義單個(gè)圖片預(yù)覽
            <van-uploader
              accept="*"
              v-model="imgList"
              :after-read="afterRead"
              :before-read="beforeRead"
              preview-size='25vw'
              @click-preview="handleclicksc"
              :before-delete="afterDelete"
              :preview-full-image="false"
              :disabled="isUploading?true:false"
              />
<!--點(diǎn)擊圖片或視頻出現(xiàn)放大圖片或播放視頻的彈窗-->
    <van-overlay :show="show" @click="show = false">
      <div class="wrapper" >
        <div class="img-block">
          <img v-if="urlType==='image'"   :src="url">
          <video autoplay class="video" v-if="urlType==='video'" :src="url" controls></video>
        </div>
<!--給視頻添加關(guān)閉圖標(biāo)-->
          <img v-if="urlType==='video'"
            @click="show=false"
            class="video-delete"
            src="./close.png"
          />
        </div>
    </van-overlay>
data(){
    return {
        isUploading:false,
        allInfoList:[],
        url:'',  //彈窗展示的圖片/視頻路徑
        urlType:'',  //彈窗展示的類型
    }
 },
  watch: {
  //監(jiān)聽allInfoList,根據(jù)allInfoList動態(tài)地修改文件列表
    'allInfoList' () {
      this.imgList = []
      for (let item of this.allInfoList) {
        let data = {
          type: item.type,
          name: item.originalName,
          url: item.type === 'video' ? 'https://replacement.png' : 'https://' + item.fileUri
        }
        this.imgList.push(data)
      }
    }
  },
methods:{
//限制上傳的內(nèi)容為視頻或圖片
    beforeRead (file) {
      if (!file.type.startsWith('image') && !file.type.startsWith('video')) {
        this.$toast('請上傳圖片或視頻')
        return false
      }
      return true
    },
    afterRead (file) {
      file.status = 'uploading'
      file.message = '上傳中...'
      //添加上傳狀態(tài),避免用戶在上傳未完成時(shí)點(diǎn)擊提交按鈕
      this.isUploading = true
      fileApi
        .uploadFile(file.file)
        .then(res => {
          if (res.data.status === 'success') {
            let fileDTO = response.data.fileDTO
            //為返回的數(shù)據(jù)添加文件類型,后面依據(jù)此來判斷
            if (file.file.type.startsWith('video')) {
              fileDTO.type = 'video'
            }
            if (file.file.type.startsWith('image')) {
              fileDTO.type = 'image'
            }
            //將返回的所有數(shù)據(jù)都保存起來(文件地址,文件名等)
            this.allInfoList.push(fileDTO)
          } else {
          //上傳失敗要清空數(shù)組,不然失敗的文件依舊會展示
            this.handleDelete(file.file.name)
          }
          file.status = ''
          file.message = ''
          this.isUploading = false
        })
        .catch((error) => {
          console.log(error)
          this.handleDelete(file.file.name)
          file.status = ''
          file.message = ''
          this.isUploading = false
        })
    },
    //根據(jù)文件名來查找到文件列表中要刪除的文件
    handleDelete (name) {
      this.imgList.forEach((item, index) => {
        if (item.file.name === name) {
          this.imgList.splice(index, 1)
        }
      })
    },
    //手動點(diǎn)擊刪除,修改包含所有信息的文件列表,通過watch根據(jù)該列表動態(tài)修改圖片文件列表
    afterDelete (file) {
      let name = file.name
      this.allInfoList.forEach((item, index) => {
        if (item.originalName === name) {
          this.allInfoList.splice(index, 1)
        }
      })
      return true
    },
   }
   //取消掉組件自帶的點(diǎn)擊預(yù)覽功能,自己添加(系統(tǒng)自帶預(yù)覽點(diǎn)擊視頻時(shí)會先視頻的播放圖片)
   handleclicksc (file) {
      let name = file.name
      for (let item of this.allInfoList) {
        if (item.type === 'video' && item.originalName === name) {
          this.url = this.getUrl(item.fileUri)
          this.urlType = 'video'
          this.show = true
        }
        if (item.type === 'image' && item.originalName === name) {
          this.url = this.getUrl(item.fileUri)
          this.urlType = 'image'
          this.show = true
        }
      }
    },
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }
  .img-block {
    position: relative;
    img{
      z-index: 99;
      max-width: 100%;
      height: auto;
      object-fit: cover;
    }
    video{
      width: 100%;
      max-height: 100vh;
    }
  }
  .video-delete{
    width: 45px;
    position: absolute;
    top: 60px;
    left: calc(100vw - 60px);
  }

Vant上傳壓縮圖片;多圖片壓縮上傳

vant這種上傳方式是一個(gè)一個(gè)讀取的,需要將多文件上傳給禁用了,multiple=“false”——使用這個(gè)屬性

根據(jù)實(shí)際情況測試,7Mb圖片結(jié)果為100+kb,個(gè)別不代表全部,也有壓縮完為400多k的,壓縮后體積還是很小的,圖片也比較清晰,提前給大家一個(gè)參考

  <van-uploader :after-read="afterRead"  :accept="'image/*'" v-model="fileList" multiple="false"
                               :max-count="4"
                />
return {
      // 圖片信息
      files: {
        name: "",
        type: ""
      }
    // 單圖片上傳圖片
    afterRead(file) {
      this.files.name = file.file.name // 獲取文件名
      this.files.type = file.file.type // 獲取類型
      this.imgPreview(file.file)
    },
// 如果是多圖片按照這種寫法即可,不過需要在上面將multiple設(shè)置為true
// afterRead(file,name) { 方法參數(shù)設(shè)置為多參數(shù)
//        if (file instanceof Array && file.length) { // 判斷是否是多圖上傳 多圖則循環(huán)添加進(jìn)去
//            file.forEach(item => {
//                    this.imgPreview(item.file)
//            })
//        } else {
//            this.imgPreview(file.file)
//        }
// },
   
    // 處理圖片
    imgPreview(file) {
      let self = this
      let Orientation
      //去獲取拍照時(shí)的信息,解決拍出來的照片旋轉(zhuǎn)問題   npm install exif-js --save   這里需要安裝一下包
      Exif.getData(file, function() {
        Orientation = Exif.getTag(this, 'Orientation')
      })
      // 看支持不支持FileReader
      if (!file || !window.FileReader) return
      if (/^image/.test(file.type)) {
        // 創(chuàng)建一個(gè)reader
        let reader = new FileReader()
        // 將圖片2將轉(zhuǎn)成 base64 格式
        reader.readAsDataURL(file)
        // 讀取成功后的回調(diào)
        reader.onloadend = function() {
          let result = this.result
          let img = new Image()
          img.src = result
          //判斷圖片是否大于500K,是就直接上傳,反之壓縮圖片
          if (this.result.length <= 500 * 1024) {
            // 上傳圖片
            self.postImg(this.result)
          } else {
            img.onload = function() {
              let data = self.compress(img, Orientation)
              // 上傳圖片
              self.postImg(data)
            }
          }
        }
      }
    },
    // 壓縮圖片
    compress(img, Orientation) {
      let canvas = document.createElement('canvas')
      let ctx = canvas.getContext('2d')
      //瓦片canvas
      let tCanvas = document.createElement('canvas')
      let tctx = tCanvas.getContext('2d')
      // let initSize = img.src.length;
      let width = img.width
      let height = img.height
      //如果圖片大于四百萬像素,計(jì)算壓縮比并將大小壓至400萬以下
      let ratio
      if ((ratio = (width * height) / 4000000) > 1) {
        // console.log("大于400萬像素");
        ratio = Math.sqrt(ratio)
        width /= ratio
        height /= ratio
      } else {
        ratio = 1
      }
      canvas.width = width
      canvas.height = height
      //    鋪底色
      ctx.fillStyle = '#fff'
      ctx.fillRect(0, 0, canvas.width, canvas.height)
      //如果圖片像素大于100萬則使用瓦片繪制
      let count
      if ((count = (width * height) / 1000000) > 1) {
        // console.log("超過100W像素");
        count = ~~(Math.sqrt(count) + 1) //計(jì)算要分成多少塊瓦片
        //      計(jì)算每塊瓦片的寬和高
        let nw = ~~(width / count)
        let nh = ~~(height / count)
        tCanvas.width = nw
        tCanvas.height = nh
        for (let i = 0; i < count; i++) {
          for (let j = 0; j < count; j++) {
            tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh)
            ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)
          }
        }
      } else {
        ctx.drawImage(img, 0, 0, width, height)
      }
      //修復(fù)ios上傳圖片的時(shí)候 被旋轉(zhuǎn)的問題
      if (Orientation != '' && Orientation != 1) {
        switch (Orientation) {
          case 6: //需要順時(shí)針(向左)90度旋轉(zhuǎn)
            this.rotateImg(img, 'left', canvas)
            break
          case 8: //需要逆時(shí)針(向右)90度旋轉(zhuǎn)
            this.rotateImg(img, 'right', canvas)
            break
          case 3: //需要180度旋轉(zhuǎn)
            this.rotateImg(img, 'right', canvas) //轉(zhuǎn)兩次
            this.rotateImg(img, 'right', canvas)
            break
        }
      }
      //進(jìn)行最小壓縮
      let ndata = canvas.toDataURL('image/jpeg', 0.2)
      tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0
      console.log(ndata)
      return ndata
    },
    // 旋轉(zhuǎn)圖片
    rotateImg(img, direction, canvas) {
      //最小與最大旋轉(zhuǎn)方向,圖片旋轉(zhuǎn)4次后回到原方向
      const min_step = 0
      const max_step = 3
      if (img == null) return
      //img的高度和寬度不能在img元素隱藏后獲取,否則會出錯(cuò)
      let height = img.height
      let width = img.width
      let step = 2
      if (step == null) {
        step = min_step
      }
      if (direction == 'right') {
        step++
        //旋轉(zhuǎn)到原位置,即超過最大值
        step > max_step && (step = min_step)
      } else {
        step--
        step < min_step && (step = max_step)
      }
      //旋轉(zhuǎn)角度以弧度值為參數(shù)
      let degree = (step * 90 * Math.PI) / 180
      let ctx = canvas.getContext('2d')
      switch (step) {
        case 0:
          canvas.width = width
          canvas.height = height
          ctx.drawImage(img, 0, 0)
          break
        case 1:
          canvas.width = height
          canvas.height = width
          ctx.rotate(degree)
          ctx.drawImage(img, 0, -height)
          break
        case 2:
          canvas.width = width
          canvas.height = height
          ctx.rotate(degree)
          ctx.drawImage(img, -width, -height)
          break
        case 3:
          canvas.width = height
          canvas.height = width
          ctx.rotate(degree)
          ctx.drawImage(img, -width, 0)
          break
      }
    },
    //將base64轉(zhuǎn)換為文件
    dataURLtoFile(dataurl) {
      var arr = dataurl.split(','),
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new File([u8arr], this.files.name, {
        type: this.files.type
      })
    },
    // 提交圖片到后端
    postImg(base64) {
      let file = this.dataURLtoFile(base64)
      // 然后壓縮后的圖片放入集合,根據(jù)自己業(yè)務(wù)調(diào)用然后一起上傳
      this.fileCompressList.push(file)
    },

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue使用rules實(shí)現(xiàn)表單字段驗(yàn)證

    vue使用rules實(shí)現(xiàn)表單字段驗(yàn)證

    這篇文章主要為大家詳細(xì)介紹了vue使用rules實(shí)現(xiàn)表單字段驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解從零搭建 vue2 vue-router2 webpack3 工程

    詳解從零搭建 vue2 vue-router2 webpack3 工程

    本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue中axios處理http發(fā)送請求的示例(Post和get)

    vue中axios處理http發(fā)送請求的示例(Post和get)

    本篇文章主要介紹了vue中axios處理http請求的示例(Post和get),這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 尤雨溪開發(fā)vue?dev?server理解vite原理

    尤雨溪開發(fā)vue?dev?server理解vite原理

    這篇文章主要為大家介紹了尤雨溪開發(fā)的玩具vite,vue-dev-server來理解vite原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • vue.js獲得當(dāng)前元素的文字信息方法

    vue.js獲得當(dāng)前元素的文字信息方法

    下面小編就為大家分享一篇vue.js獲得當(dāng)前元素的文字信息方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue-resourse將json數(shù)據(jù)輸出實(shí)例

    vue-resourse將json數(shù)據(jù)輸出實(shí)例

    這篇文章主要為大家詳細(xì)介紹了vue-resourse將json數(shù)據(jù)輸出實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 在vue3項(xiàng)目中給頁面添加水印的實(shí)現(xiàn)方法

    在vue3項(xiàng)目中給頁面添加水印的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹一下在vue3項(xiàng)目中添加水印的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧
    2023-08-08
  • JavaScript 沙箱探索

    JavaScript 沙箱探索

    這篇文章主要介紹了JavaScript 沙箱探索,沙箱是基于 event bus 形式的通信實(shí)現(xiàn)上層的功能,文章的例子選擇接口實(shí)現(xiàn)了 web worker 與 quickjs 的 EventEmitter,,需要的朋友可以參考一下
    2021-10-10
  • vue element-ui v-for循環(huán)el-upload上傳圖片 動態(tài)添加、刪除方式

    vue element-ui v-for循環(huán)el-upload上傳圖片 動態(tài)添加、刪除方式

    這篇文章主要介紹了vue element-ui v-for循環(huán)el-upload上傳圖片 動態(tài)添加、刪除方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue使用Canvas在畫布上添加圖片方式

    vue使用Canvas在畫布上添加圖片方式

    這篇文章主要介紹了vue使用Canvas在畫布上添加圖片方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評論

诸城市| 微博| 武冈市| 丽水市| 新郑市| 同德县| 宿州市| 柳河县| 潞城市| 麦盖提县| 伊宁县| 札达县| 花垣县| 上饶市| 嵊泗县| 邢台市| 林周县| 正阳县| 建昌县| 西城区| 临澧县| 辽阳县| 锦屏县| 南安市| 伊宁县| 昌邑市| 民丰县| 兴安县| 蚌埠市| 永靖县| 长葛市| 昌图县| 祁东县| 礼泉县| 肇东市| 揭阳市| 罗田县| 清涧县| 土默特左旗| 高青县| 金川县|