当前位置:网站首页>腾讯云,实现图片上传
腾讯云,实现图片上传
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>
图片:
各位看观,持续更新中。
边栏推荐
- Blue bridge - maximum common divisor and minimum common multiple
- Azkaban概述
- Scientific research: are women better than men?
- 丸子百度小程序详细配置教程,审核通过。
- openresty ngx_lua执行阶段
- Pytest (5) - assertion
- openresty ngx_ Lua execution phase
- Last words record
- SFTP cannot connect to the server # yyds dry goods inventory #
- Leetcode takes out the least number of magic beans
猜你喜欢
Video display and hiding of imitation tudou.com
Design and implementation of community hospital information system
【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
Avoid material "minefields"! Play with super high conversion rate
Character painting, I use characters to draw a Bing Dwen Dwen
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
Openresty ngx Lua Execution stage
[download white paper] does your customer relationship management (CRM) really "manage" customers?
Master Fur
随机推荐
100 basic multiple choice questions of C language (with answers) 04
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Start the remedial work. Print the contents of the array using the pointer
Introduce reflow & repaint, and how to optimize it?
Official announcement! The third cloud native programming challenge is officially launched!
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
Azkaban实战
Erreur de type de datagramme MySQL en utilisant Druid
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
Avoid material "minefields"! Play with super high conversion rate
Introduce reflow & repaint, and how to optimize it?
Richview trvunits image display units
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Design and implementation of community hospital information system
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
openresty ngx_ Lua variable operation
Traditional chips and AI chips
Asp+access campus network goods trading platform