-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
153 lines (137 loc) · 3.3 KB
/
index.html
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cloud-image-upload</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#upload{
margin: auto;
height: 100px;
width: 100px;
border-radius: 50%;
border: 4px solid #2970f6;
font-size: 100px;
text-align: center;
color:#2970f6;
line-height: 92px;
cursor: pointer;
overflow: hidden;
position: relative;
}
#upload input {
position: absolute;
font-size: 0px;
opacity: 0;
filter: alpha(opacity=0);
cursor: pointer;
width: 100px;
height: 100px;
left: 0;
}
#preview{
width: 600px;
height: 600px;
margin: auto;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
#summit{
margin: auto;
width: 100px;
border-radius: 20px;
text-align: center;
border-width: 2px;
border-style: solid;
border-color: #2970f6;
font-size: 18px;
padding: 5px 10px;
box-sizing: border-box;
color: rgb(255, 255, 255);
background-color: #2970f6;
cursor: pointer;
}
</style>
</head>
<body>
<div id="upload" ><input id="pic" type="file" accept="image/png,image/jpg,image/jpeg" name="pic" />
+</div>
<div id="summit" onclick="upload()">上传</div>
<div id="preview"></div>
</body>
<script src="//imgcache.qq.com/qcloud/tcbjs/1.7.1/tcb.js"></script>
<script>
const app = tcb.init({
env: 'ykc-151533'
});
const auth = app.auth();
auth.anonymousAuthProvider()
.signIn() //AnonymousAuthProvider.signIn() 匿名登录云开发
.then(() => {
console.log(app.auth().hasLoginState().loginType) //登录成功
}).catch(err => {
console.log("登录失败",err) //登录失败
})
var fileinput = document.getElementById('pic'),
preview = document.getElementById('preview');
fileinput.addEventListener('change',function(){
if (!fileinput.value) {
console.log('no pic');
return;
}
else{
console.log('success')
}
var file = fileinput.files[0];
if (file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/jpg') {
alert('仅支持jpeg和png');
fileinput.value = '';
preview.style.backgroundImage = 'url(#)';
return;
}else{
var reader = new FileReader();
reader.onload = function(e){
var data = e.target.result;
preview.style.backgroundImage = 'url(' + data + ')';
}
reader.readAsDataURL(file);
console.log(file.name)
}
})
function upload(){
app
.uploadFile({
// 云端路径
cloudPath: document.getElementById('pic').files[0].name,
// 需要上传的文件,File 类型
filePath: document.getElementById('pic').files[0]
})
.then(res => {
// 返回文件 ID
console.log(res.fileID)
console.log('上传成功')
function getdownload(){
app
.getTempFileURL({
fileList: [res.fileID]
})
.then(res => {
res.fileList.forEach((el) => {
if(el.code === 'SUCCESS') {
console.log(el.tempFileURL)
} else {
console.log('无地址')//获取下载链接失败
}
})
});
}
getdownload();
})
}
</script>
</html>