Vue上傳圖片到oss

參考:九宮格(圖片上傳)

? ? ? ? ? ?實(shí)現(xiàn)oss簽名直傳

? ??????????自定義圖標(biāo)

最終樣式:


1.html代碼

<div class="uploadImage">

? ? ? ? <input

? ? ? ? ? @change="fileChange($event)"

? ? ? ? ? type="file"

? ? ? ? ? id="upload_file"

? ? ? ? ? multiple

? ? ? ? ? style="display: none"

? ? ? ? >

? ? ? ? <p class="title titlepay">請(qǐng)上傳圖片(最多3張)</p>

? ? ? ? <div class="add-img">

? ? ? ? ? <ul class="img-list">

? ? ? ? ? ? <li v-for="(url, index) in imgList" :key="index">

? ? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? <img

? ? ? ? ? ? ? ? ? class="del"

? ? ? ? ? ? ? ? ? src="../../assets/close-circle.png" //刪除的圖標(biāo)路徑

? ? ? ? ? ? ? ? ? @click.stop="delImg(index)"

? ? ? ? ? ? ? ? ? v-if="payEvidence==''"

? ? ? ? ? ? ? ? >

? ? ? ? ? ? ? ? <img class="app-bg" :src="url.file.src">

? ? ? ? ? ? ? </div>

? ? ? ? ? ? </li>

? ? ? ? ? ? <li class="add" @click="chooseType" v-show="show" v-if="payEvidence==''">

? ? ? ? ? ? ? <div class="add-image" align="center">

? ? ? ? ? ? ? ? <van-icon class="icon-camera" name="camera" custom></van-icon>?//相機(jī)的圖標(biāo)

? ? ? ? ? ? ? ? <p class="font13" style="center">添加圖片</p>

? ? ? ? ? ? ? </div>

? ? ? ? ? ? </li>

? ? ? ? ? </ul>

? ? ? ? </div>

? ? ? </div>

? ? ? <div class="btn">

? ? ? ? <van-button

? ? ? ? ? class="interval-normal-b"

? ? ? ? ? @click="submit"

? ? ? ? ? size="large"

? ? ? ? ? v-if="payEvidence==''"

? ? ? ? >確認(rèn)提交</van-button>

? ? ? </div>

? ? </div>

2.style樣式

//按鈕
.checked {

? font-size: 80px;

? color: #38bb14;

}

.van-button--large {

? height: 40px;

? line-height: 40px;

? margin-top: 30px;

}

.van-button--default {

? color: #ffff;

? background-color: #4bd126;

}

//圖片上傳

.app-bg > img {

? width: 100%;

? height: 100%;

? -webkit-transform: scale(1.03);

? -moz-transform: scale(1.03);

? -ms-transform: scale(1.03);

? -o-transform: scale(1.03);

? transform: scale(1.03);

}

.add-img {

? width: 100%;

? padding-top: 8px;

}

.add-img p {

? color: #999;

}

.add {

? display: inline-block;

? margin-bottom: 20px;

? text-align: center !important;

}

.add-image p {

? text-align: center !important;

}

.add-image {

? border: 1px dashed rgba(0, 0, 0, 0.2);

}

.add-image .icon-camera {

? margin-top: 25px;

? font-size: 30px;

}

.mui-btn-block {

? border: 0;

? border-radius: 0;

? background-color: #87582e;

? color: #fff;

? margin-bottom: 0;

? bottom: 0;

}

.mui-buttom {

? position: fixed;

? width: 100%;

? bottom: 0;

? z-index: 999;

}

/*九宮格*/

.img-list {

? overflow: hidden;

? padding: 10px 12px;

? padding-top: 0;

}

.img-list > li {

? float: left;

? width: 32%;

? text-align: center;

? padding-top: 31%;

? margin-left: 1%;

? margin-top: 1%;

? margin-bottom: 20px;

? position: relative;

}

.img-list > li > div {

? position: absolute;

? left: 0;

? top: 0;

? width: 100%;

? height: 100%;

? overflow: hidden;

}

.img-list > li > div .app-bg {

? width: 100%;

? height: 100%;

}

.mui-fullscreen {

? z-index: 9999;

}

.del {

? position: absolute;

? width: 18px;

? top: 0;

? right: 0;

? z-index: 999;

}

3.script代碼

<script>

import axios from 'axios';? //可解決測(cè)試時(shí)的跨域問(wèn)題

export default P({

? name: "x-address-edit",

? title: "圖片上傳",

? data() {

? ? return {

? ? ? imgList: [],

? ? ? size: 0,

? ? ? limit: 3, //限制圖片上傳的數(shù)量

? ? ? show: true,

? ? ? payEvidence: ""

? ? };

? },

? methods: {

? ? chooseType() {

? ? ? document.getElementById("upload_file").click();

? ? },

? ? fileChange(el) {

? ? ? if (!el.target.files[0].size) return;

? ? ? this.fileList(el.target);

? ? ? el.target.value = "";

? ? },

? ? fileList(fileList) {

? ? ? let files = fileList.files;

? ? ? for (let i = 0; i < files.length; i++) {

? ? ? ? //判斷是否為文件夾

? ? ? ? if (files[i].type != "") {

? ? ? ? ? this.fileAdd(files[i]);

? ? ? ? } else {

? ? ? ? ? //文件夾處理

? ? ? ? ? this.folders(fileList.items[i]);

? ? ? ? }

? ? ? }

? ? },

? ? //文件夾處理

? ? folders(files) {

? ? ? let _this = this;

? ? ? //判斷是否為原生file

? ? ? if (files.kind) {

? ? ? ? files = files.webkitGetAsEntry();

? ? ? }

? ? ? files.createReader().readEntries(function(file) {

? ? ? ? for (let i = 0; i < file.length; i++) {

? ? ? ? ? if (file[i].isFile) {

? ? ? ? ? ? _this.foldersAdd(file[i]);

? ? ? ? ? } else {

? ? ? ? ? ? _this.folders(file[i]);

? ? ? ? ? }

? ? ? ? }

? ? ? });

? ? },

? ? foldersAdd(entry) {

? ? ? let _this = this;

? ? ? entry.file(function(file) {

? ? ? ? _this.fileAdd(file);

? ? ? });

? ? },

? ? fileAdd(file) {

? ? ? if (this.limit !== undefined) this.limit--;

? ? ? if (this.limit !== undefined && this.limit < 0) return;

? ? ? //總大小

? ? ? this.size = this.size + file.size;

? ? ? //判斷是否為圖片文件

? ? ? if (file.type.indexOf("image") == -1) {

? ? ? ? this.$toast("請(qǐng)選擇圖片文件");

? ? ? } else {

? ? ? ? let reader = new FileReader();

? ? ? ? let image = new Image();

? ? ? ? let _this = this;

? ? ? ? reader.readAsDataURL(file);

? ? ? ? reader.onload = function() {

? ? ? ? ? file.src = this.result;

? ? ? ? ? image.onload = function() {

? ? ? ? ? ? let width = image.width;

? ? ? ? ? ? let height = image.height;

? ? ? ? ? ? file.width = width;

? ? ? ? ? ? file.height = height;

? ? ? ? ? ? _this.imgList.push({

? ? ? ? ? ? ? file

? ? ? ? ? ? });

? ? ? ? ? ? if (_this.imgList.length == 3) {

? ? ? ? ? ? ? _this.show = false;

? ? ? ? ? ? }

? ? ? ? ? ? console.log(_this.imgList);

? ? ? ? ? };

? ? ? ? ? image.src = file.src;

? ? ? ? };

? ? ? }

? ? },

? ? delImg(index) {

? ? ? this.size = this.size - this.imgList[index].file.size; //總大小

? ? ? this.imgList.splice(index, 1);

? ? ? if (this.limit !== undefined) this.limit = 6 - this.imgList.length;

? ? ? if (this.imgList.length >= 3) {

? ? ? ? this.show = false;

? ? ? } else {

? ? ? ? this.show = true;

? ? ? }

? ? },

? ? displayImg() {},

? ? submit() {

? ? ? const self = this;

? ? ? let urls=[];

? ? ? if (self.imgList.length > 0) {

? ? ? ? for (let i = 0; i < self.imgList.length; i++) {

? ? ? ? self.uploadFile(self.imgList[i].file).then(res=>{

? ? ? ? ? urls.push(res);

? ? ? ? });

? ? ? ? }

????????..........

????????...........

? ? ? ? }).then(res => {

? ? ? ? ? if (res.code === 200) {

? ? ? ? ? ? this.success();

? ? ? ? ? } else {

? ? ? ? ? ? this.$toast(res.message);

? ? ? ? ? }

? ? ? ? });

? ? ? } else {

? ? ? ? this.$toast("請(qǐng)先添加圖片");

? ? ? }

? ? },

? ? success() {

? ? ? this.$toast("提交成功");

? ? ? this.to("/allorder?type=5");

? ? },

? ? uploadFile(file) {

//xxxxxxx后端獲取簽名的接口

? ? ? this.$get("xxxxxxx").then(res => {

? ? ? ? debugger;

? ? ? ? console.log(res);

? ? ? ? let newFilename =

? ? ? ? ? Math.round(new Date().getTime() / 1000) +

? ? ? ? ? Math.ceil(Math.random() * 100000).toString() +

? ? ? ? ? file.name;

? ? ? ? var ossData = new FormData();

? ? ? ? ossData.append("OSSAccessKeyId", res.data.accessid);

? ? ? ? ossData.append("policy", res.data.policy);

? ? ? ? ossData.append("Signature", res.data.signature);

? ? ? ? //上傳阿里云需要一個(gè)動(dòng)態(tài)的random

? ? ? ? ossData.append("key", res.data.dir + newFilename);

? ? ? ? ossData.append("success_action_status", 200); // 指定返回的狀態(tài)碼

? ? ? ? ossData.append("file", file,file.name);

? ? ? ? axios.post(res.data.host, ossData, {

? ? ? ? ? headers: {

? ? ? ? ? ? "Content-Type": "multipart/form-data"

? ? ? ? ? }

? ? ? ? })

? ? ? ? ? .then(ret => {

? ? ? ? ? ? console.log(res.data.host+'/'+res.data.dir + newFilename);

? ? ? ? ? ? return res.data.host+'/'+res.data.dir + newFilename;

? ? ? ? ? })

? ? ? ? ? .catch(err => {

? ? ? ? ? ? console.log(err);

? ? ? ? ? });

? ? ? });

? ? }

? },

? created() {

????? this.payEvidence = this.q.payEvidence;

? ? if (this.payEvidence != "") {

? ? ? this.payEvidence.split(";").forEach(item => {

? ? ? ? if (item != "") {

? ? ? ? ? this.imgList.push({ file: { src: item } });

? ? ? ? }

? ? ? });

? ? }

? }

});

</script>


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末分冈,一起剝皮案震驚了整個(gè)濱河市猛频,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異咏窿,居然都是意外死亡砖第,警方通過(guò)查閱死者的電腦和手機(jī)撤卢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)梧兼,“玉大人放吩,你說(shuō)我怎么就攤上這事∮鸾埽” “怎么了渡紫?”我有些...
    開(kāi)封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)考赛。 經(jīng)常有香客問(wèn)我惕澎,道長(zhǎng),這世上最難降的妖魔是什么颜骤? 我笑而不...
    開(kāi)封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任唧喉,我火速辦了婚禮,結(jié)果婚禮上忍抽,老公的妹妹穿的比我還像新娘欣喧。我一直安慰自己,他們只是感情好梯找,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著益涧,像睡著了一般锈锤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上闲询,一...
    開(kāi)封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天久免,我揣著相機(jī)與錄音,去河邊找鬼扭弧。 笑死阎姥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鸽捻。 我是一名探鬼主播呼巴,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼御蒲!你這毒婦竟也來(lái)了衣赶?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤厚满,失蹤者是張志新(化名)和其女友劉穎府瞄,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碘箍,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡遵馆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年鲸郊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片货邓。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡秆撮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出逻恐,到底是詐尸還是另有隱情像吻,我是刑警寧澤,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布复隆,位于F島的核電站拨匆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏挽拂。R本人自食惡果不足惜惭每,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亏栈。 院中可真熱鬧台腥,春花似錦、人聲如沸绒北。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)闷游。三九已至峻汉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間脐往,已是汗流浹背休吠。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留业簿,地道東北人瘤礁。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像梅尤,于是被迫代替她去往敵國(guó)和親柜思。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容