1.安装qiniu-js
npm install qiniu-js
2.引入qiniu-js上传模块
import * as qiniu from 'qiniu-js'
3.上传文件到七牛云
html:
<el-upload :http-request="sliderRequest" name="file" accept='video/*' >
<el-button size="small" type="primary">点击上传</el-button>
</el-upload>
js:
methods:{
sliderRequest(upload) {
let uploadKey =new Date().getTime() + upload.file.name; //上传七牛云需要的key
var self_ = this;
//向后台获取到上传七牛云的token;
gzhApi.qiToken().then(res => {
if (res.code == 200) {
let qiniuToken = res.data;
//向七牛云发起上传
const observable = qiniu.upload(upload.file, uploadKey, qiniuToken)
observable.subscribe(() => {}, () => {}, (e) => {
//uploadResult 就是你文件存储在七牛云地址了
let uploadResult= `http://qhd4en5tw.hn-bkt.clouddn.com/${e.key}`;
});
}
})
}
}