当前位置:网站首页>腾讯云,实现图片上传
腾讯云,实现图片上传
2022-07-05 02:43:00 【13少啊】
在上传图片的时候,没有现在的接口,这个时候可以实现第三方上传,这里介绍一下腾讯云上传图片。
一.配置一个腾讯云
1.注册账号
...直接登录官网。
2.创建一个存储桶
3.创建跨域规则
密钥的作用:
上传文件的时候需要密钥。
二.element-ui上传文件
<el-upload
class="avatar-uploader"
action="https://jsonplaceholder.typicode.com/posts/"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
重要的参数:
文件上传的方法
上传成功
覆盖默认行为,自定义上传
注意点:
http-request:action失效,和上传成功之后的值也会失效。
三.腾讯云上传使用
1.安装插件
cos-js-sdk-v5 yarn add cos-js-sdk-v5
2:导入
import Cos from 'cos-js-sdk-v5'
3:实例化
let cos = new Cos({
SecretId: 'COS_SECRETID', // 身份识别 ID
SecretKey: 'COS_SECRETKEY', // 身份密钥
});
直接搜索密匙获取:
4:使用
cos.putObject({
Bucket: 'XXXXXXXXXXXXXXXXXXX', /* 存储桶: */
Region: 'ap-guangzhou', /* 存储桶所在地域,必须字段 */
Key: uid+'', /* 必须是字符串 */
StorageClass: 'STANDARD', // 固定值
Body: 文件对象, // 上传文件对象
onProgress: (progressData)=> {
// 上传进度
console.log(JSON.stringify(progressData));
}
}, (err, data)=> {
// 上传成功或者失败的回调
console.log(err || data);
});
Bucket:
所在的位置:
Key:
使用文件对象里面的uid
Region:
Region: 'ap-chongqing',
四.实际操作
<template>
<div class="">
<el-upload
class="avatar-uploader"
action="#"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
:http-request="httpRequest"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
</template>
<script>
import Cos from 'cos-js-sdk-v5'
let cos = new Cos({
SecretId:'xxxxxxxxxxxxxxxxxxxxxxx', // 身份识别 ID
SecretKey:'xxxxxxxxxxxxxxxxx', // 身份密钥
});
export default {
data() {
return {
imageUrl: ''
};
},
methods: {
httpRequest(res){
cos.putObject({
Bucket: 'xxxxxxxxxxxx', /* 存储桶: */
Region: 'ap-chongqing', /* 存储桶所在地域,必须字段 */
Key: res.file.uid+'', /* 必须是字符串 */
StorageClass: 'STANDARD', // 固定值
Body:res.file, // 上传文件对象
onProgress: (progressData)=> {
// 上传进度
console.log(JSON.stringify(progressData));
}
},(err,data)=>{
// console.log(err||data);
this.imageUrl='http://'+data.Location
});
},
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
const isPng=file.type==='image/png'
const limit=file.size/1024/1024<1
if (!isPng) {
alert('请上传png图片')
}
if (!limit) {
alert('请上传1M内图片')
}
return isPng && limit
}
}
}
</script>
<style>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409EFF;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
图片:
各位看观,持续更新中。
边栏推荐
- The database and recharge are gone
- Blue bridge - maximum common divisor and minimum common multiple
- Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
- Eight days of learning C language - while loop (embedded) (single chip microcomputer)
- Master Fur
- 打破信息茧房-我主动获取信息的方法 -#3
- Richview trvunits image display units
- Azkaban实战
- Learn game model 3D characters, come out to find a job?
- Unpool(nn.MaxUnpool2d)
猜你喜欢
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
2021 Li Hongyi machine learning (3): what if neural network training fails
Hmi-31- [motion mode] solve the problem of picture display of music module
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
丸子百度小程序详细配置教程,审核通过。
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Missile interception -- UPC winter vacation training match
Video display and hiding of imitation tudou.com
Scientific research: are women better than men?
Flume配置4——自定义MYSQLSource
随机推荐
Start the remedial work. Print the contents of the array using the pointer
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
[technology development-26]: data security of new information and communication networks
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
返回二叉树中两个节点的最低公共祖先
2022/02/13
Cut! 39 year old Ali P9, saved 150million
Kotlin - coroutine
低度酒赛道进入洗牌期,新品牌如何破局三大难题?
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
100 basic multiple choice questions of C language (with answers) 04
How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)
openresty ngx_ Lua variable operation
【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
Vb+access hotel service management system
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
A label making navigation bar
Talk about the things that must be paid attention to when interviewing programmers
Master Fur
丸子百度小程序详细配置教程,审核通过。