1.先选择图片然后上传到云存储
//选择图片
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success:(res)=>{
wx.showLoading({
title: '上传中',
})
//选中图片的路径
const filePath = res.tempFilePaths[0]
//上传图片到云存储中的路径
const cloudPath = `my-image${filePath.match(/\.[^.]+?$/)[0]}`
wx.cloud.uploadFile({
filePath,
cloudPath
}).then(res=>{
wx.hideLoading()
app.globalData.fileID = res.fileID
app.globalData.cloudPath = cloudPath
app.globalData.imagePath = filePath
wx.navigateTo({
url: '../storageConsole/storageConsole'
})
}).catch(err=>{
wx.hideLoading()
console.error('[上传文件] 失败:', e)
wx.showToast({
icon: 'none',
title: '上传失败',
})
})
获取图片的临时链接:
//从云存储获取临时url
const fileid = "cloud://dev-0g4h9exve5c944d3.6465-dev-0g4h9exve5c944d3-1306228026/my-image.png"
wx.cloud.getTempFileURL({
fileList:[fileid]
}).then(res=>{
console.log(res)
let image = res.fileList[0].tempFileURL
console.log(image)
})
删除文件
//删除文件
const fileid = "cloud://dev-0g4h9exve5c944d3.6465-dev-0g4h9exve5c944d3-1306228026/my-image.png"
wx.cloud.deleteFile({
fileList:[fileid]
}).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
网友评论