微信小程序-云開發(fā)(2)選擇上傳換路徑下載刪除圖片

wxml:
<view>
    <image src="{{imgUrl}}" mode="aspectFit"/>
  <button bindtap="sel">選擇圖片</button>
  <button bindtap="upload">上傳圖片</button>
    <!-- <image src="https://7465-test-1adh8-1259161555.tcb.qcloud.la/images/1?sign=0fec6515dc17db97123585c0a0a46589&t=1556961798"/> -->
  <button bindtap="download">下載上傳的圖片</button>
  <button bindtap="getUrl">根據(jù)id換取圖片路徑</button>
    <image src="{{downImgUrl}}"/>
  <button bindtap="delFile">刪除文件</button>
</view>
js:
data: {
    imgUrl:'../../images/bf.jpg',
    downImgUrl:'',
    fileId:''
},
onLoad: function() {
    this.db = wx.cloud.database();
    this.mycol = this.db.collection("022project");
},
//選擇圖片
sel(){
    var that = this;
    wx.chooseImage({//wx.chooseImage
        success(res){
            console.log("選擇圖片成功!",res)
            that.setData({
                imgUrl:res.tempFilePaths[0]
            })
        }
    })
},
//上傳圖片
upload(){
    var that = this;
    wx.showToast({
        title:"上傳中",
        icon:"loading"
    })
    wx.cloud.uploadFile({
        cloudPath:"images/1",//可以不寫后綴,會自動創(chuàng)建到云儲存中
        filePath:that.data.imgUrl,
        success(res){
            wx.showToast({
                title:"上傳成功",
                icon:"success",
                duration:2000
            })
            that.setData({
                fileId:res.fileID
            })
            console.log("圖片上傳成功!",res)
        }
    })
},
//下載上傳的圖片
download(){
    wx.cloud.downloadFile({
        fileID:this.data.fileId
    }).then(res=>{
        console.log("下載成功!",res)//
    })
},
//換取路徑
getUrl(){
    var that = this;
    wx.cloud.getTempFileURL({
        //--fileList: 數(shù)組元素為需要換取文件路徑的id值
        fileList:["cloud://test-1adh8.7465-test-1adh8/images/1"],
        success(res){
            console.log("獲取真實(shí)路徑",res)//返回的是圖片下載地址
            that.setData({
                downImgUrl:res.fileList[0].tempFileURL
            })
        }
    })
},
//刪除文件
delFile(){
    var that = this;
    wx.cloud.deleteFile({
        fileList:["cloud://test-1adh8.7465-test-1adh8/images/1"],
        success(res){
            console.log("刪除文件:",res)
            that.setData({
                downImgUrl:res.fileList[0]//TODO
            })
        }
    })
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容