elementui 上传七牛_element ui使用上传组件上传文件到七牛(qiniu-js)

本文介绍了如何结合Element UI组件与七牛云服务,实现在前端直接上传文件到七牛云存储。首先,需要在七牛云注册并获取AccessKey和SecretKey。然后,通过后端接口生成上传Token,前端使用此Token进行文件上传。前端页面通过Element UI的上传组件,设定上传URL和验证文件类型、大小。当上传成功后,将返回的key拼接成资源路径并保存。
摘要由CSDN通过智能技术生成

博主正在重构博客中,刚开始时静态资源都是上传到本地服务器的,但这个项目博主最后打算真正上线运营的。索性就改进了下,把静态资源尽量放到云存储中,方便后续开发。这里把方法和遇到坑给记录下。

1.使用前提注册七牛云并创建存储空间

在秘钥管理中拿到 AccessKey/SecretKey

2.工作原理

上传文件到七牛有客户端上传和服务器上传两种方式,这里我们选择客户端上传,上传前从后端获取token,再通过客服端直接上传。获得上传成功后的key值,拼接上路径前缀,即是文件的资源路径,再将资源路径存入到数据库中。

3.后端接口搭建npm install qiniuimport Router from 'koa-router'

import axios from './utils/axios'

import qiniu from 'qiniu'

//需要填写你的 Access Key 和 Secret Key

const accessKey = '你的 Access Key'

const secretKey = '你的 Secret Key'

//要上传的空间

const bucket = '要上传的空间'

//声明路由

let router = new Router({ prefix: '/qiniu' })

//登录接口

router.post('/upload', async (ctx, next) => {

//上传到七牛后保存的文件名

let key = ctx.request.body.key

//生成上传 Token

// console.log(key, bucket)

let mac = new qiniu.auth.digest.Mac(accessKey, secretKey)

let putPolicy = new qiniu.rs.PutPolicy({ scope: bucket })

// 生成上传文件的 token

let uptoken = putPolicy.uploadToken(mac)

if (uptoken) {

ctx.body = {

code: 0,

msg: '获取上传token成功',

uptoken

}

} else {

ctx.body = {

code: -1,

msg: '获取token失败'

}

}

})

export default router

更多功能可参考官方SDK文档

4.前端页面搭建

上传文件组件,根据存储区域不同设置action(具体可通过产品手册查看),请求携带参数通过data绑定

class="avatar-uploader"

id="imguplad"

action="https://upload.qiniup.com"

:data="qn"

:show-file-list="false"

:on-success="handleAvatarSuccess"

:before-upload="beforeAvatarUpload"

>

//七牛图片上传

qn: {

token: '',

key: ''

}

上传前先进行文件检查//上传封面前检查

async beforeAvatarUpload(file) {

const isJPG = file.type === 'image/jpeg'

const isGIF = file.type === 'image/gif'

const isPNG = file.type === 'image/png'

const isBMP = file.type === 'image/bmp'

if (!isJPG && !isGIF && !isPNG && !isBMP) {

this.$message.error('上传图片必须是JPG/GIF/PNG/BMP 格式!')

return false

}

const isLt2M = file.size / 1024 / 1024 < 2

if (!isLt2M) {

this.$message.error('上传头像图片大小不能超过 2MB!')

}

//根据文件名生成上传唯一key值

let key =

'blog/image/' +

this.utils.formatDate(new Date().getTime(), 'YY/MM/DD/hh:mm:ss/') +

file.name

console.log(key)

await this.getuploadToken(key)

return (isJPG || isGIF || isPNG || isBMP) && isLt2M

}

同时获取token,根据日期时间设置唯一key值//获取七牛上传token

getuploadToken: async function(key) {

const { status, data } = await this.$axios.post('/qiniu/upload', {

key

})

// console.log(status, data)

if (status == 200 && data.uptoken) {

this.qn.token = data.uptoken

this.qn.key = key

// console.log(this.qn)

}

}

上传成功后将返回的key值拼接为真正的url资源路径,并设置到addpost.post_bg上,让img标签正常显示图片//返回上传的图片地址

handleAvatarSuccess(res, file) {

this.addpost.post_bg = 'https://gravatar.catbk.cn/' + res.key

}

效果大概这样

d9dbc23ca38eccee5dee263e804f0f79.png文章列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值