前言
我們經(jīng)常將文件(音頻、圖片、壓縮包、文檔)存儲在網(wǎng)上,我們的云開發(fā)平臺為開發(fā)者提供“云存儲”空間,開發(fā)者只需將文件上傳,就可以得到這個(gè)文件的下載地址和File ID。
一、云存儲的使用
代碼如下(示例):
wx.cloud.uploadFile({
cloudPath:`上傳的位置/$文件的命名`,
filePath:臨時(shí)文件路徑,
success(res){
//成功后的回調(diào)
},
fail(res){
//失敗后的回調(diào)
}
})
二、使用演練
1.上傳圖片到云存儲中
效果:
wxml示例:
<view class="group">
<text>附加圖片:</text>
<view class="img">
<view class="img_row">
<block wx:for="{{cloudImages}}" wx:key="index">
<image class="img01" src="{{item}}"></image>
</block>
</view>
<image class="img01" src="../../static/camera.png" bindtap="imgFile"></image>
</view>
</view>
js代碼示例:
imgFile(){
var that=this
wx.chooseImage({
count:2, //上傳圖片最多不超2張
success(res){
/* console.log(res) */
for(var i=0;i<res.tempFilePaths.length;i++){
wx.cloud.uploadFile({
cloudPath:`actionInfo/${Math.random()}_${Date.now()}.${res.tempFilePaths[i].match(/\.(\w+)$/)[1]}`,
filePath:res.tempFilePaths[i],
success(res){
/* console.log(res) */
that.data.cloudImages.push(res.fileID)
that.setData({
cloudImages:that.data.cloudImages
})
/* console.log(that.data.cloudImages) */
}
})
}
}
})
},
2.下載并保存圖片到手機(jī)
代碼如下(示例):文章來源:http://www.zghlxwxcb.cn/news/detail-513348.html
downLoadImage(event){
wx.cloud.downloadFile({
fileID:event.currentTarget.dataset.id,
success(res){
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(){
wx.showToast({
title: '保存成功',
})
}
})
}
})
},
tips:上邊的鏈接下載地址時(shí)FileID,如果鏈接下載圖片地址是url,則調(diào)用的API是:wx.downloadFile({url:})
wx.saveImageToPhotosAlbum:將圖片保存到手機(jī)中文章來源地址http://www.zghlxwxcb.cn/news/detail-513348.html
到了這里,關(guān)于微信小程序云開發(fā)之云存儲(實(shí)現(xiàn)圖片上傳和下載)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!