(1)绑定上传地址,上传数据对象
<el-upload class="upload-demo" :action="uploadUrl" :data="uploadData"
:on-success="handleSuccess" :file-list="[]" :show-file-list="false" :limit="1">
</el-upload>
(2)定义数据
// 商户的driver
driver: null,
// 图片上传的路径
uploadUrl: '',
// 上传图片的携带的信息
uploadData: {},
// 图片的链接头部分
cdn: ""
(3)定义方法文章来源:https://www.toymoban.com/news/detail-694124.html
图片的路径就是图片头加上返回的key文章来源地址https://www.toymoban.com/news/detail-694124.html
/**
* 获取获取商户的driver
*/
async getDriver() {
let res = await customerService.getDriver();
if (res.code == 200) {
this.driver = res.data.cdn_driver;
this.cdn = res.data.cdn;
this.driver ? this.getUploadToken() : "";
}
},
/**
* 上传图片获取token
*/
async getUploadToken() {
let params = {
driver: this.driver
};
let res = await customerService.getUploadToken(params);
if (res.code == 200) {
switch (this.driver) {
case 'oss':
this.uploadData = {
OSSAccessKeyId: res.data.accessid,
policy: res.data.policy,
success_action_status: '200',
callback: res.data.callback,
signature: res.data.signature,
dir: res.data.dir
};
this.uploadUrl = res.data.host;
break;
case 'qiniu':
this.uploadData = {
dir: '',
token: res.data.token
};
this.uploadUrl = "https://up.qiniup.com";
}
}
},
/**
* 上传图片成功
*/
handleSuccess(res, file, fileList) {
// 图片的路径就是图片头加上返回的key
let imgUrl = `${this.cdn}${res.key}`;
}
到了这里,关于el-upload上传图片到七牛云或阿里云的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!