客户端使用七牛云上传文件

客户端上传文件

首先从后台后去七牛云的token,这里使用node来获取token

1
2
3
4
5
6
7
8
9
10
11
12
const getUploadToken = () => {
const accessKey = 'accessKey';
const secretKey = 'secretKey';
const bucket = "bucket"; //存储空间名称
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
const options = {
scope: bucket
}
const putPolicy = new qiniu.rs.PutPolicy(options);
const uploadToken = putPolicy.uploadToken(mac);
return uploadToken;
}

接着再浏览器请求接口获取七牛云token,进行文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!--使用的vue+elementUI-->
qiniuUpload() {
this.axios.get("/api/work/getQiniuToken").then(res => {
let token = res.token;
let config = {
useCdnDomain: true,
region: qiniu.region.z1
};
let putExtra = {
fname: "",
params: {},
mimeType: null
};
let file = this.form.audio[0].raw;
let key = dayjs().format("YYYYMMDDHHmmssSSS") + ".mp3";
let observable = qiniu.upload(file, key, token, putExtra, config);
let that = this;
let observer = {
next: res => {
console.log(res);
},
error: err => {
console.log(err);
},
complete: res => {
that.submitForm(res.key);
}
};
let subscription = observable.subscribe(observer);
});
},