vue上傳圖片到oss的方法示例(圖片帶有刪除功能)
最近Vue項(xiàng)目中,要將用戶上傳的圖片全部上傳到oss上,
OSS配置項(xiàng)請(qǐng)?jiān)L問(wèn):https://help.aliyun.com/document_detail/64095.html?spm=a2c4g.11186623.6.773.kcD20n
OSS平臺(tái)配置
在平臺(tái)的概覽里面看看自己的基礎(chǔ)設(shè)置里面的讀寫(xiě)權(quán)限是否改為了公共讀,我這邊只有配置公共讀才上傳并且回顯圖片成功,其他情況還請(qǐng)朋友告知,謝謝
關(guān)于跨域訪問(wèn)的配置

這里是我的效果圖 (當(dāng)只有點(diǎn)擊上傳按鈕時(shí)才會(huì)上傳到OSS)

預(yù)覽圖片
<template>
<div class="vue-uploader">
<div class="file-list">
<section v-for="(file, index) of files" class="file-item draggable-item" :key="file.name">
<img :src="file.src" alt="" ondragstart="return false;">
<span class="file-remove" @click="remove(index)">+</span>
</section>
<section v-if="status == 'ready'" class="file-item">
<div @click="add" class="add"></div>
</section>
</div>
<section v-if="files.length != 0" class="upload-func">
<div class="progress-bar">
<section v-if="uploading" :width="(percent * 100) + '%'">{{(percent * 100) + '%'}}</section>
</div>
<div class="operation-box">
<button v-if="status == 'ready'" @click="submit">上傳</button>
<button v-if="status == 'finished'" @click="finished">完成</button>
</div>
</section>
<input type="file" @change="fileChanged" ref="file" multiple="multiple" accept="image/jpg,image/jpeg,image/png,image/bmp">
</div>
</template>
<script>
export default {
data() {
return {
status: 'ready',
files: [],
point: {},
uploading: false,
percent: 0
}
},
methods: {
add() {
this.$refs.file.click()
},
submit() {
console.log(this.files)
// if (this.files.length === 0) {
// console.warn('no file!');
// return
// }
var that=this
// 這里是OSS
const client = new OSS.Wrapper({
region: 'oss-cn-hangzhou',
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name'
});
const fNum = this.files;
for(var i=0;i<fNum.length;i++){
var f=fNum[i].file
console.log(f)
const Name=f.name
console.log(Name)
const suffix = Name.substr(Name.indexOf("."));
const obj=this.timestamp();
const storeAs = 'header/'+obj+suffix // 路徑+時(shí)間戳+后綴名
console.log(storeAs)
client.multipartUpload(storeAs, f).then(function (result){
console.log(result.res.requestUrls)
})
}
},
// 時(shí)間戳
timestamp:function(){
const time = new Date();
const y = time.getFullYear();
const m = time.getMonth()+1;
const d = time.getDate();
const h = time.getHours();
const mm = time.getMinutes();
const s = time.getSeconds();
const ms = time.getMilliseconds()
return ""+y+this.Add0(m)+this.Add0(d)+this.Add0(h)+this.Add0(mm)+this.Add0(s)+this.Add0(ms);
},
Add0:function(m){
return m<10?'0'+m : m;
} ,
finished() {
this.files = []
this.status = 'ready'
},
remove(index) {
this.files.splice(index, 1)
},
fileChanged() {
const list = this.$refs.file.files
for (let i = 0; i < list.length; i++) {
if (!this.isContain(list[i])) {
const item = {
name: list[i].name,
size: list[i].size,
file: list[i]
}
this.html5Reader(list[i], item)
this.files.push(item)
}
}
this.$refs.file.value = ''
},
// 將圖片文件轉(zhuǎn)成BASE64格式
html5Reader(file, item){
const reader = new FileReader()
reader.onload = (e) => {
this.$set(item, 'src', e.target.result)
}
reader.readAsDataURL(file)
},
isContain(file) {
return this.files.find((item) => item.name === file.name && item.size === file.size)
},
}
}
</script>
<style>
.vue-uploader {
border: 1px solid #e5e5e5;
}
.vue-uploader .file-list {
padding: 10px 0px;
}
.vue-uploader .file-list:after {
content: '';
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
font-size: 0;
}
.vue-uploader .file-list .file-item {
float: left;
margin-left: 10px;
position: relative;
width: 150px;
text-align: center;
}
.vue-uploader .file-list .file-item img{
width: 150px;
height: 150px;
border: 1px solid #ececec;
}
.vue-uploader .file-list .file-item .file-remove {
position: absolute;
right: 4px;
display: none;
top: 4px;
width: 20px;
height: 20px;
font-size:20px;
text-align: center;
color: white;
cursor: pointer;
line-height: 20px;
border-radius: 100%;
transform: rotate(45deg);
background: rgba(0, 0, 0, 0.5);
}
.vue-uploader .file-list .file-item:hover .file-remove {
display: inline;
}
.vue-uploader .file-list .file-item .file-name {
margin: 0;
height: 40px;
word-break: break-all;
font-size: 14px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.vue-uploader .add {
width: 150px;
height: 150px;
float: left;
text-align: center;
line-height: 150px;
font-size: 30px;
cursor: pointer;
background: url(../assets/upImg.png) no-repeat; // 這里使用的是我本地圖片
background-size: 100% 100%;
}
.vue-uploader .upload-func {
display: flex;
padding: 10px;
margin: 0px;
background: #f8f8f8;
border-top: 1px solid #ececec;
}
.vue-uploader .upload-func .progress-bar {
flex-grow: 1;
}
.vue-uploader .upload-func .progress-bar section {
margin-top: 5px;
background: #00b4aa;
border-radius: 3px;
text-align: center;
color: #fff;
font-size: 12px;
transition: all .5s ease;
}
.vue-uploader .upload-func .operation-box {
flex-grow: 0;
padding-left: 10px;
}
.vue-uploader .upload-func .operation-box button {
padding: 4px 12px;
color: #fff;
background: #007ACC;
border: none;
border-radius: 2px;
cursor: pointer;
}
.vue-uploader > input[type="file"] {
display: none;
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-router3.0版本中 router.push 不能刷新頁(yè)面的問(wèn)題
這篇文章主要介紹了vue-router3.0版本中 router.push 不能刷新頁(yè)面的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
vue-cli3.0 腳手架搭建項(xiàng)目的過(guò)程詳解
這篇文章主要介紹了vue-cli3.0 腳手架搭建項(xiàng)目的過(guò)程,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
Vue 動(dòng)態(tài)路由的實(shí)現(xiàn)及 Springsecurity 按鈕級(jí)別的權(quán)限控制
這篇文章主要介紹了Vue 動(dòng)態(tài)路由的實(shí)現(xiàn)以及 Springsecurity 按鈕級(jí)別的權(quán)限控制的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
公共Hooks封裝文件下載useDownloadFile實(shí)例詳解
這篇文章主要為大家介紹了公共Hooks封裝文件下載useDownloadFile實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue3中el-uplod結(jié)合ts實(shí)現(xiàn)圖片粘貼上傳
本文主要介紹了vue3中el-uplod結(jié)合ts實(shí)現(xiàn)圖片粘貼上傳,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Vue導(dǎo)入Echarts實(shí)現(xiàn)散點(diǎn)圖
這篇文章主要為大家詳細(xì)介紹了Vue導(dǎo)入Echarts實(shí)現(xiàn)散點(diǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-12-12

