微信小程序 云开发 -- 图片的上传、查看
- 上传
// 上传图片
doUpload: function() {
//当前系统的时间,用于组成图片的名称
var time = util.formatTime(new Date());
// 选择图片
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function(res) {
wx.showLoading({
title: '上传中',
})
const filePath = res.tempFilePaths[0]
// 上传图片
const cloudPath = time + filePath.match(/\.[^.]+?$/)[0]
wx.cloud.uploadFile({
cloudPath,
filePath,
success: res => {
wx.showToast({
title: '[上传文件] 成功',
})
//唯一的fileID,用于标识唯一性
console.log("fileID", res.fileID)
},
fail: e => {
wx.showToast({
icon: 'none',
title: '上传失败',
})
},
complete: () => {
wx.hideLoading()
}
})
},
fail: e => {
console.error(e)
}
})
},
- 查看
//fileid :上传图片成功之后,返回的fileid
data: {
fileid:'cloud://tww-b2adbf.7477-tww-b2adbf/20181010111230.jpg'
},
<image class='view_item_child_image' src='{{fileid}}'></image>