【微信开发】小程序对云存储中文件的上传、取得cloudID、下载地址(URL)、删除文件
【微信开发】小程序对云存储中文件的上传、取得cloudID、下载地址(URL)、删除文件一、上传;二、获取下载地址(URL);三、删除文件;
·
一、上传;
二、获取下载地址(URL);
三、删除文件;
wxml:
<button type="default" bindtap="chooseFile">上传文件</button>
<button type="warn" bindtap="deleteFile">删除文件</button>
<button type="default" bindtap="getURL">获取URL</button>
一、上传:
//选择文件
chooseFile(){
let that = this;
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album'],
success(res) {
console.log("打开相册成功!",res);
that.upLoadFile(res.tempFiles[0].tempFilePath);
},
fail(err){
console.log("打开相册失败!",err);
},
complete(){}
})
},
//上传文件到云存储
upLoadFile(tempFilePath){
let that = this;
wx.showLoading({
title: '上传中....',
mask: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {
wx.cloud.uploadFile({
filePath : tempFilePath,//本地文件地址
cloudPath : 'testDemo.jpg',//云存储地址
success(res){
console.log("上传文件到云存储成功!",res);
that.setData({
fileID : res.fileID
})
wx.showToast({
title: '上传成功!',
duration: 1200,
icon: 'success',
//image: 'image',
mask: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {},
})
},
fail(err){
console.log("上传文件到云存储失败!",err);
},
complete(){
wx.hideLoading({
noConflict: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {},
})
}
})
},
})
},
上传成功:
二、获取下载地址(URL):
//获取云存储的下载地址
getURL(){
let that = this;
let fileID = that.data.fileID;
wx.showLoading({
title: '获取URL中...',
mask: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {
wx.cloud.getTempFileURL({
fileList : [{
fileID : fileID
}],
success(res){
console.log("获取云存储的下载地址成功!",res);
that.setData({
currentCloudPath_URL : e.fileList[0].tempFileURL//取得当前上传文件的URL
})
wx.showToast({
title: '获取云存储的下载地址成功!',
duration: 1200,
icon: 'success',
//image: 'image',
mask: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {},
})
},
fail(err){
console.log("获取云存储的下载地址失败!",err);
},
complete(){
wx.hideLoading({
noConflict: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {},
})
},
})
},
})
},
获取成功:
三、删除文件:
//删除文件
deleteFile(){
let that = this;
let fileID = that.data.fileID;
wx.showLoading({
title: '删除中...',
mask: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {
wx.cloud.deleteFile({
fileList : [fileID],//填入你的fileID(cloudID),可以填多个,如:['cloudID','cloudID','cloudID']
success(res){
console.log("删除成功!",res);
wx.showToast({
title: '删除成功!',
duration: 1200,
icon: 'success',
//image: 'image',
mask: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {},
})
},
fail(err){
console.log("删除失败!",err);
},
complete(){
wx.hideLoading({
noConflict: true,
success: (res) => {},
fail: (res) => {},
complete: (res) => {},
})
}
})
},
})
},
更多推荐
已为社区贡献3条内容
所有评论(0)