一、思路,從后臺獲取七牛token,上傳圖片到七牛,獲取返回圖片路徑放入el-upload。
二、代碼。
<el-input v-model="listVideoQuery.orgLogo" @change="orgLogoChange"></el-input> <el-col :span="10" class="mt10"> <el-upload class="upload-demo" :file-list="fileList2" :action="domain" :before-upload="beforeAvatarUpload" :data="form" :on-remove="handleRemoveQrgLogo" :on-success="handleAvatarSuccess" list-type="picture" :limit="1" multiple> <el-button size="small" type="primary" @click="upPicName='orgLogo'">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> </el-upload> </el-col>export default { name: 'edit', data() { return { upPicName:'', //上傳圖片名字 fileList1:[], form: {}, //上傳到七牛的token bucketQuery:{ bucket: 'website-image' //這是我用來獲取token傳給后臺的字段 }, domain: 'http://upload.qiniu.com', // 七牛云的上傳地址 qiniuaddr: 'pd6rnk9ck.bkt.clouddn.com' // 這是七牛云空間的外鏈默認域名 } }, methods: { // 上傳圖片規格 beforeAvatarUpload (file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 5 if (!isJPG) { return this.$message.error('上傳文件只能是 JPG或PNG 格式!') } if (!isLt2M) { return this.$message.error('上傳圖片大小不能超過 5MB!') } return upQiniu(this.bucketQuery).then(res => { this.form = { token:res.data } }) }, //圖片改變及上傳 bannerPicChange(value) { this.fileList1 = [] this.fileList1.push({ name: value, url: value }) }, handleBannerPicRemove(file, fileList) { this.fileList1 = [] this.listVideoQuery.orgLogo= '' }, handleAvatarSuccess(response) { if(this.upPicName =='orgLogo'){ this.fileList1 = [] this.fileList1.push({ name: 'http://' + this.qiniuaddr + '/' + response.key, url: 'http://' + this.qiniuaddr + '/' + response.key }) this.listVideoQuery.orgLogo= 'http://' + this.qiniuaddr + '/' + response.key } this.$message({ type: 'success', message: '上傳成功!', duration: 2000 }) }, } }
三、效果。
總結
以上所述是小編給大家介紹的vue中el-upload上傳圖片到七牛的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答