vue3.0中使用web云开发(6)
腾讯云开发文件上传(图片)
-
今天在通过腾讯云进行开发时遇到了一个问题比较难处理,那就是文件上传
-
通过看官方的文档发现还是有些地方感觉不是那么容易懂,而且还被文档的一个注释给误导了,唉
整个代码大致是这样的
uploadImg() {
this.app.uploadFile({
cloudPath: `${this.state.id}.jpg`,
filePath: this.state.fileUrl.files[0]
}).then((res) => {
this.app.getTempFileURL({
fileList: [{
fileID: res.fileID,
tempFileURL: '',
maxAge: 120 * 60 * 1000
}]
}).then((res) => {
this.db.collection('register')
.doc(this.state.id)
.update({
userImg: res.fileList[0].tempFileURL
}).then((res) => {
console.log(res)
})
})
})
}
上传文件
this.app.uploadFile({
cloudPath: `${this.state.id}.jpg`,
filePath: this.state.fileUrl.files[0]
}).then((res) => {
console.log(res)
}
-
cloudPath
指的是云端储存路径,如果你储存的是图片别忘了后面的格式 -
filePath
这个比较终于(也就是你在本地获取的文件) -
res
中有个res.fileID
获取临时路径时有用
获取临时路径
this.app.getTempFileURL({
fileList: [{
fileID: res.fileID,
tempFileURL: '',
maxAge: 120 * 60 * 1000
}]
}).then((res) => {
console.log(res)
}
在res中可以获取临时文件路径