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

vue實現(xiàn)簡單圖片上傳功能

 更新時間:2022年03月02日 08:48:49   作者:Cryday  
這篇文章主要為大家詳細介紹了vue實現(xiàn)簡單圖片上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)簡單圖片上傳的具體代碼,供大家參考,具體內容如下

就是給自己留個參照,有什么不合理的地方請大家指出來,然后調整

1.效果展示

2.html相關的代碼展示

<div class="form-list">
? ? ? ?<label class="label-one">商品圖片</label>
? ? ? ?<div class="add-picture">
? ? ? ? ?<ul>
? ? ? ? ? <li v-if="img_li1">
? ? ? ? ? ? ? <!-- 展示圖片 -->
? ? ? ? ? ? ? <div class="hasPic" v-if="img_1">
? ? ? ? ? ? ? ? ? ? ? <img class="seledPic" :src="this.imgdata.seledPic_1" />
? ? ? ? ? ? ? ? ? ? ? <img
? ? ? ? ? ? ? ? ? ? ? ? class="closepic"
? ? ? ? ? ? ? ? ? ? ? ? src="../../assets/images/close.png"
? ? ? ? ? ? ? ? ? ? ? ? @click="pichide('seledPic_1')"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? <div class="selPic" v-else>
? ? ? ? ? ? ? ? ? ? ? <label for="picadd" class="picadd"></label>
? ? ? ? ? ? ? ? ? ? ? <input
? ? ? ? ? ? ? ? ? ? ? ? id="picadd"
? ? ? ? ? ? ? ? ? ? ? ? type="file"
? ? ? ? ? ? ? ? ? ? ? ? maxlength
? ? ? ? ? ? ? ? ? ? ? ? class="input-file"
? ? ? ? ? ? ? ? ? ? ? ? multiple="multiple"
? ? ? ? ? ? ? ? ? ? ? ? @change="onUpload($event,'seledPic_1')"
? ? ? ? ? ? ? ? ? ? ? ? accept="image/*"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? </li>
? ? ? ? ? ? ? ? ? <li v-if="img_li2">
? ? ? ? ? ? ? ? ? ? <!-- 展示圖片 -->
? ? ? ? ? ? ? ? ? ? <div class="hasPic" v-if="img_2">
? ? ? ? ? ? ? ? ? ? ? <img class="seledPic" :src="this.imgdata.seledPic_2" />
? ? ? ? ? ? ? ? ? ? ? <img
? ? ? ? ? ? ? ? ? ? ? ? class="closepic"
? ? ? ? ? ? ? ? ? ? ? ? src="../../assets/images/close.png"
? ? ? ? ? ? ? ? ? ? ? ? @click="pichide('seledPic_2')"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? <div class="selPic" v-else>
? ? ? ? ? ? ? ? ? ? ? <label for="picadd" class="picadd"></label>
? ? ? ? ? ? ? ? ? ? ? <input
? ? ? ? ? ? ? ? ? ? ? ? id="picadd"
? ? ? ? ? ? ? ? ? ? ? ? type="file"
? ? ? ? ? ? ? ? ? ? ? ? maxlength
? ? ? ? ? ? ? ? ? ? ? ? class="input-file"
? ? ? ? ? ? ? ? ? ? ? ? multiple="multiple"
? ? ? ? ? ? ? ? ? ? ? ? @change="onUpload($event,'seledPic_2')"
? ? ? ? ? ? ? ? ? ? ? ? accept="image/*"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? </li>
? ? ? ? ? ? ? ? ? <li v-if="img_li3">
? ? ? ? ? ? ? ? ? ? <!-- 展示圖片 -->
? ? ? ? ? ? ? ? ? ? <div class="hasPic" v-if="img_3">
? ? ? ? ? ? ? ? ? ? ? <img class="seledPic" :src="this.imgdata.seledPic_3" />
? ? ? ? ? ? ? ? ? ? ? <img
? ? ? ? ? ? ? ? ? ? ? ? class="closepic"
? ? ? ? ? ? ? ? ? ? ? ? src="../../assets/images/close.png"
? ? ? ? ? ? ? ? ? ? ? ? @click="pichide('seledPic_3')"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? <div class="selPic" v-else>
? ? ? ? ? ? ? ? ? ? ? <label for="picadd" class="picadd"></label>
? ? ? ? ? ? ? ? ? ? ? <input
? ? ? ? ? ? ? ? ? ? ? ? id="picadd"
? ? ? ? ? ? ? ? ? ? ? ? type="file"
? ? ? ? ? ? ? ? ? ? ? ? maxlength
? ? ? ? ? ? ? ? ? ? ? ? class="input-file"
? ? ? ? ? ? ? ? ? ? ? ? multiple="multiple"
? ? ? ? ? ? ? ? ? ? ? ? @change="onUpload($event,'seledPic_3')"
? ? ? ? ? ? ? ? ? ? ? ? accept="image/*"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? </li>
? ? ? ? ? ? ? ? </ul>
? ? ? ? ? ? ? ? <p class="prompt">提示:最多添加3張圖片,格式為jpg或png</p>
? ? ? ? ? ? <p class="prompt" v-if="tips">店鋪照片不能為空</p>
? ? </div>
</div>

3.css樣式代碼

.form-list {
? ? width: 100%;
? ? color: #666;
? ? font-size: 16px;
? ? margin: 0 0 20px 0;
}
.add-picture {
? ? overflow: hidden;
}
?
.add-picture ul li {
? ? float: left;
? ? margin: 0 20px 10px 0;
}
?
.add-picture .hasPic,
.add-picture .selPic {
? ? overflow: hidden;
? ? width: 86px;
? ? height: 86px;
? ? border-radius: 4px;
}
?
.add-picture .hasPic {
? ? position: relative;
}
?
.add-picture .hasPic img {
? ? display: block;
? ? width: 100%;
? ? height: 100%;
}
?
.add-picture .hasPic img.closepic {
? ? position: absolute;
? ? top: 0;
? ? right: 0;
? ? display: block;
? ? width: 25px;
? ? height: 25px;
}
?
.add-picture .selPic .picadd {
? ? display: block;
? ? width: 100%;
? ? height: 100%;
? ? background: url("../../assets/images/picadd.png");
? ? background-size: 100% 100%;
}
?
.add-picture .selPic input {
? ? display: none;
}
?
.add-picture .prompt {
? ? clear: both;
? ? margin: 0;
? ? font-size: 14px;
? ? color: #ff0000;
}

4、js相關代碼

export default {
? ?data() {
? ? ? ? return {
? ? ? ? ? ?// ?上傳圖片標識
? ? ? ? ? ?img_1: false,
? ? ? ? ? ?img_2: false,
? ? ? ? ? ?img_3: false,
? ? ? ? ? ?imgdata: {
? ? ? ? ? ? ? ? seledPic_1: "",
? ? ? ? ? ? ? ? seledPic_2: "",
? ? ? ? ? ? ? ? seledPic_3: ""
? ? ? ? ? ? ? ? },
? ? ? ? ? ?img_li1: true,
? ? ? ? ? ?img_li2: false,
? ? ? ? ? ?img_li3: false,
? ? ? ? }
? ? },
?methods: {
? ? ? // 判斷圖片上傳類型
? ? ? produceImg(type, url) {
? ? ? ? let that = this;
? ? ? ? if (type == "seledPic_1") {
? ? ? ? ? that.img_1 = true;
? ? ? ? ? that.img_li2 = true;
? ? ? ? ? that.$set(that.imgdata, "seledPic_1", url);
? ? ? ? } else if (type == "seledPic_2") {
? ? ? ? ? that.img_2 = true;
? ? ? ? ? that.img_li3 = true;
? ? ? ? ? that.$set(that.imgdata, "seledPic_2", url);
? ? ? ? } else if (type == "seledPic_3") {
? ? ? ? ? that.img_3 = true;
? ? ? ? ? that.$set(that.imgdata, "seledPic_3", url);
? ? ? ? }
? ? ? },
? ? ? // 點擊關閉按鈕圖片隱藏
? ? ? pichide(type) {
? ? ? ? let that = this;
? ? ? ? if (type == "seledPic_1") {
? ? ? ? ? if (that.imgdata.seledPic_1 != "") {
? ? ? ? ? ? that.img_1 = false;
? ? ? ? ? ? that.img_li2 = false;
? ? ? ? ? }
? ? ? ? ? if (that.imgdata.seledPic_2 != "") {
? ? ? ? ? ? that.img_1 = false;
? ? ? ? ? ? that.img_li2 = true;
? ? ? ? ? }
? ? ? ? ? if (that.imgdata.seledPic_3 != "") {
? ? ? ? ? ? that.img_1 = false;
? ? ? ? ? ? that.img_li2 = true;
? ? ? ? ? ? that.img_li3 = true;
? ? ? ? ? }
? ? ? ? } else if (type == "seledPic_2") {
? ? ? ? ? if (that.imgdata.seledPic_1 != "") {
? ? ? ? ? ? that.img_2 = false;
? ? ? ? ? }
? ? ? ? ? if (that.imgdata.seledPic_2 != "") {
? ? ? ? ? ? that.img_2 = false;
? ? ? ? ? ? that.img_li3 = false;
? ? ? ? ? }
? ? ? ? ? if (that.imgdata.seledPic_3 != "") {
? ? ? ? ? ? that.img_2 = false;
? ? ? ? ? ? that.img_li3 = true;
? ? ? ? ? }
? ? ? ? } else if (type == "seledPic_3") {
? ? ? ? ? that.img_3 = false;
? ? ? ? }
? ? ? },
? ? ? //start 上傳圖片
? ? ? onUpload(e, type) {
? ? ? ? let file = e.target.files[0];
? ? ? ? let filesize = file.size;
? ? ? ? let filename = file.name;
? ? ? ? if (filesize > 10485760) {
? ? ? ? ? alert("圖片太大,無法上傳");
? ? ? ? } else {
? ? ? ? ? let reader = new FileReader();
? ? ? ? ? // 將圖片轉為base64位
? ? ? ? ? reader.readAsDataURL(file);
? ? ? ? ? reader.onload = function(k) {
? ? ? ? ? ? // 讀取到的圖片base64 數(shù)據(jù)編碼
? ? ? ? ? ? var imgcode = k.target.result;
? ? ? ? ? ? let data = {
? ? ? ? ? ? ? image: imgcode
? ? ? ? ? ? };
? ? ? ? ? ? axios({ ? ? ? ? ? ? ?
? ? ? ? ? ? ? url: "http://………………………………",//url地址
? ? ? ? ? ? ? method: "POST",
? ? ? ? ? ? ? data: qs.stringify(data)
? ? ? ? ? ? })
? ? ? ? ? ? ? .then(res => {
? ? ? ? ? ? ? ? let resdata = res.data;
? ? ? ? ? ? ? ? if (resdata.code == 200) {
? ? ? ? ? ? ? ? ? let url = resdata.info;
? ? ? ? ? ? ? ? ? this.produceImg(type, url);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? alert(resdata.msg);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? })
? ? ? ? ? ? ? .catch(err => {
? ? ? ? ? ? ? ? console.log(err);
? ? ? ? ? ? ? });
? ? ? ? ? }.bind(this);
? ? ? ? }
? ? ? },
? ? ? //end結束圖片
? ?}
}?

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Vue.js單向綁定和雙向綁定實例分析

    Vue.js單向綁定和雙向綁定實例分析

    這篇文章主要介紹了Vue.js單向綁定和雙向綁定,結合實例形式分析了vue.js單向綁定與雙向綁定相關原理、實現(xiàn)方法及操作技巧,需要的朋友可以參考下
    2018-08-08
  • vue Tooltip提示動態(tài)換行問題

    vue Tooltip提示動態(tài)換行問題

    這篇文章主要介紹了vue Tooltip提示動態(tài)換行問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue點擊自增和求和的實例代碼

    vue點擊自增和求和的實例代碼

    今天小編就為大家分享一篇vue點擊自增和求和的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue中提示$index is not defined錯誤的解決方式

    vue中提示$index is not defined錯誤的解決方式

    這篇文章主要介紹了vue中提示$index is not defined錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue中如何實現(xiàn)變量和字符串拼接

    vue中如何實現(xiàn)變量和字符串拼接

    這篇文章主要介紹了vue中如何實現(xiàn)變量和字符串拼接,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • vue如何加載本地json數(shù)據(jù)

    vue如何加載本地json數(shù)據(jù)

    這篇文章主要介紹了vue如何加載本地json數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Springboot運用vue+echarts前后端交互實現(xiàn)動態(tài)圓環(huán)圖

    Springboot運用vue+echarts前后端交互實現(xiàn)動態(tài)圓環(huán)圖

    我們做項目的時候,常常需要一些統(tǒng)計圖來展示我們的數(shù)據(jù),作為web開發(fā)人員,會實現(xiàn)統(tǒng)計圖是我們必會的技能。我將帶大家來實現(xiàn)動態(tài)餅圖的實現(xiàn),感興趣的可以了解一下
    2021-06-06
  • vue、element實現(xiàn)表格表頭縱向顯示方式

    vue、element實現(xiàn)表格表頭縱向顯示方式

    這篇文章主要介紹了vue、element實現(xiàn)表格表頭縱向顯示方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue2滾動條加載更多數(shù)據(jù)實現(xiàn)代碼

    vue2滾動條加載更多數(shù)據(jù)實現(xiàn)代碼

    本篇文章主要介紹了vue2滾動條加載更多數(shù)據(jù)實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • 在Vue中定義和調用過濾器的方法

    在Vue中定義和調用過濾器的方法

    過濾器(Filters)是 vue 為開發(fā)者提供的功能,常用于文本的格式化,這篇文章主要介紹了如何在Vue中定義和調用過濾器,需要的朋友可以參考下
    2023-09-09

最新評論

仪陇县| 黎城县| 定远县| 安达市| 连城县| 伊川县| 南郑县| 合川市| 若尔盖县| 南漳县| 峡江县| 博罗县| 芮城县| 广南县| 梁山县| 正镶白旗| 白玉县| 鄂托克旗| 晴隆县| 荥阳市| 和田县| 乐平市| 古蔺县| 和平区| 通许县| 西充县| 柳州市| 台湾省| 广西| 襄垣县| 准格尔旗| 新绛县| 原平市| 深州市| 辽宁省| 辽阳县| 西乌珠穆沁旗| 泸西县| 莒南县| 玉林市| 神池县|