欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用iView Upload 組件實(shí)現(xiàn)手動(dòng)上傳圖片的示例代碼

 更新時(shí)間:2018年10月01日 11:06:55   作者:與蟒唯舞  
這篇文章主要介紹了使用iView Upload 組件實(shí)現(xiàn)手動(dòng)上傳圖片的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在過(guò)去,瀏覽器是不允許我們讀取本地的文件,包括圖片。因此,當(dāng)我們需要預(yù)覽一個(gè)圖片的時(shí)候,往往先將它傳送到服務(wù)端,然后服務(wù)端返回一個(gè)訪問(wèn) url 地址,達(dá)到預(yù)覽圖片的功能。如今,隨著標(biāo)準(zhǔn)不斷的改善,JavaScript 里的 API 越來(lái)越多,我們可以通過(guò)直接讀取本地文件的方式來(lái)加載我們想要看到的文本或者圖片,一定程度上減少了服務(wù)端的壓力。

Upload 組件參考文檔:https://www.iviewui.com/components/upload

文檔提供的參考代碼:

<template>
  <div class="demo-upload-list" v-for="item in uploadList">
    <template v-if="item.status === 'finished'">
      ![](item.url)
      <div class="demo-upload-list-cover">
        <Icon type="ios-eye-outline" @click.native="handleView(item.name)"></Icon>
        <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
      </div>
    </template>
    <template v-else>
      <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
    </template>
  </div>
  <Upload
    ref="upload"
    :show-upload-list="false"
    :default-file-list="defaultList"
    :on-success="handleSuccess"
    :format="['jpg','jpeg','png']"
    :max-size="2048"
    :on-format-error="handleFormatError"
    :on-exceeded-size="handleMaxSize"
    :before-upload="handleBeforeUpload"
    multiple
    type="drag"
    action="http://jsonplaceholder.typicode.com/posts/"
    style="display: inline-block;width:58px;">
    <div style="width: 58px;height:58px;line-height: 58px;">
      <Icon type="camera" size="20"></Icon>
    </div>
  </Upload>
  <Modal title="查看圖片" v-model="visible">
    ![]('https://o5wwk8baw.qnssl.com/' + imgName + '/large')
  </Modal>
</template>
<script>
  export default {
    data () {
      return {
        defaultList: [
          {
            'name': 'a42bdcc1178e62b4694c830f028db5c0',
            'url': 'https://o5wwk8baw.qnssl.com/a42bdcc1178e62b4694c830f028db5c0/avatar'
          },
          {
            'name': 'bc7521e033abdd1e92222d733590f104',
            'url': 'https://o5wwk8baw.qnssl.com/bc7521e033abdd1e92222d733590f104/avatar'
          }
        ],
        imgName: '',
        visible: false,
        uploadList: []
      }
    },
    methods: {
      handleView (name) {
        this.imgName = name;
        this.visible = true;
      },
      handleRemove (file) {
        // 從 upload 實(shí)例刪除數(shù)據(jù)
        const fileList = this.$refs.upload.fileList;
        this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
      },
      handleSuccess (res, file) {
        // 因?yàn)樯蟼鬟^(guò)程為實(shí)例,這里模擬添加 url
        file.url = 'https://o5wwk8baw.qnssl.com/7eb99afb9d5f317c912f08b5212fd69a/avatar';
        file.name = '7eb99afb9d5f317c912f08b5212fd69a';
      },
      handleFormatError (file) {
        this.$Notice.warning({
          title: '文件格式不正確',
          desc: '文件 ' + file.name + ' 格式不正確,請(qǐng)上傳 jpg 或 png 格式的圖片。'
        });
      },
      handleMaxSize (file) {
        this.$Notice.warning({
          title: '超出文件大小限制',
          desc: '文件 ' + file.name + ' 太大,不能超過(guò) 2M。'
        });
      },
      handleBeforeUpload () {
        const check = this.uploadList.length < 5;
        if (!check) {
          this.$Notice.warning({
            title: '最多只能上傳 5 張圖片。'
          });
        }
        return check;
      }
    },
    mounted () {
      this.uploadList = this.$refs.upload.fileList;
    }
  }
</script>
<style>
  .demo-upload-list{
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list img{
    width: 100%;
    height: 100%;
  }
  .demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list:hover .demo-upload-list-cover{
    display: block;
  }
  .demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
</style>

自己實(shí)現(xiàn)手動(dòng)上傳:

<template>
  <div>
    <div class="demo-upload-list" v-for="item in uploadList">
      ![](item.url)
      <div class="demo-upload-list-cover">
        <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
      </div>
    </div>
    <Upload ref="upload" :show-upload-list="false" :format="['jpg','jpeg','png']" :max-size="2048" :before-upload="handleBeforeUpload" :on-format-error="handleFormatError" :on-exceeded-size="handleMaxSize" type="drag" action="http://jsonplaceholder.typicode.com/posts/" style="display: inline-block;width:58px;">
      <div style="width: 58px;height:58px;line-height: 58px;">
        <Icon type="camera" size="20"></Icon>
      </div>
    </Upload>
  </div>
</template>
<script>
export default {
 methods: {
  data(){
    return {
      uploadList: []
    }
  },
  handleBeforeUpload(file) {
    // 創(chuàng)建一個(gè) FileReader 對(duì)象
    let reader = new FileReader()
    // readAsDataURL 方法用于讀取指定 Blob 或 File 的內(nèi)容
    // 當(dāng)讀操作完成,readyState 變?yōu)?DONE,loadend 被觸發(fā),此時(shí) result 屬性包含數(shù)據(jù):URL(以 base64 編碼的字符串表示文件的數(shù)據(jù))
    // 讀取文件作為 URL 可訪問(wèn)地址
    reader.readAsDataURL(file)

    const _this = this
    reader.onloadend = function (e) {
      file.url = reader.result
      _this.uploadList.push(file)
    }
  },
  handleRemove(file) {
    this.uploadList.splice(this.uploadList.indexOf(file), 1)
  },
  handleFormatError(file) {
   this.$Notice.warning({
    title: '文件格式不正確',
    desc: '文件 ' + file.name + ' 格式不正確,請(qǐng)上傳 jpg 或 png 格式的圖片。'
   })
  },
  handleMaxSize(file) {
   this.$Notice.warning({
    title: '超出文件大小限制',
    desc: '文件 ' + file.name + ' 太大,不能超過(guò) 2M。'
   })
  }
 }
}
</script>
<style scoped>
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
  margin-right: 4px;
}

.demo-upload-list img {
  width: 100%;
  height: 100%;
}

.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, .6);
}

.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}

.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}

.ivu-icon {
  line-height: 58px;
}
</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue2.x安裝并使用SCSS的全部過(guò)程

    Vue2.x安裝并使用SCSS的全部過(guò)程

    這篇文章主要給大家介紹了關(guān)于Vue2.x安裝并使用SCSS的相關(guān)資料,以及如何在vue 2.x中全局引用公共scss文件,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • 使用Vue制作圖片輪播組件思路詳解

    使用Vue制作圖片輪播組件思路詳解

    這篇文章主要介紹了使用Vue制作圖片輪播組件思路詳解,需要的朋友可以參考下
    2018-03-03
  • 一文詳解Vue中的虛擬DOM與Diff算法

    一文詳解Vue中的虛擬DOM與Diff算法

    vue中的diff算法時(shí)常是面試過(guò)程中的考點(diǎn),本文將為大家講解何為diff以及diff算法的實(shí)現(xiàn)過(guò)程,那么在了解diff之前,我們需要先了解虛擬DOM是什么,需要的朋友可以參考下
    2024-02-02
  • 手把手教你使用寶塔部署Vue項(xiàng)目

    手把手教你使用寶塔部署Vue項(xiàng)目

    這篇文章主要給大家介紹了關(guān)于如何使用寶塔部署Vue項(xiàng)目的相關(guān)資料,寶塔面板提供了非常方便的方式來(lái)部署 Vue 項(xiàng)目,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Vue的路由動(dòng)態(tài)重定向和導(dǎo)航守衛(wèi)實(shí)例

    Vue的路由動(dòng)態(tài)重定向和導(dǎo)航守衛(wèi)實(shí)例

    下面小編就為大家分享一篇Vue的路由動(dòng)態(tài)重定向和導(dǎo)航守衛(wèi)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue中axios的二次封裝實(shí)例講解

    vue中axios的二次封裝實(shí)例講解

    在本篇文章里小編給大家整理了關(guān)于vue中axios的二次封裝實(shí)例以及相關(guān)知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。
    2019-10-10
  • Vue實(shí)現(xiàn)表格數(shù)據(jù)的增刪改查的示例代碼

    Vue實(shí)現(xiàn)表格數(shù)據(jù)的增刪改查的示例代碼

    Vue是一個(gè)用于構(gòu)建用戶界面的JavaScript框架,在Vue中可以通過(guò)使用Vue組件來(lái)實(shí)現(xiàn)對(duì)表格的增刪改查操作,下面將介紹一些基礎(chǔ)的Vue組件和技術(shù)來(lái)實(shí)現(xiàn)對(duì)表格的增刪改查操作,需要的朋友可以參考下
    2024-08-08
  • vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說(shuō)明

    vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說(shuō)明

    這篇文章主要介紹了vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 關(guān)于eslint+prettier+husky的配置及說(shuō)明

    關(guān)于eslint+prettier+husky的配置及說(shuō)明

    這篇文章主要介紹了關(guān)于eslint+prettier+husky的配置及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 從源碼里了解vue中的nextTick的使用

    從源碼里了解vue中的nextTick的使用

    這篇文章主要介紹了vue的nextTick的使用,本文從源碼出發(fā)給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11

最新評(píng)論