The code for uploading files to the cloud in the WeChat applet is as follows, which are the codes of wxml and js respectively
The following code is the code in wxml
<!--miniprogram/pages/home/home.wxml-->
<button size='mini' bindtap="chooseFile">选择文件 </button>
<text>{
{sourceName}}</text>
<button size='mini' bindtap="uploadFile">上传文件</button>
The following code is the code in the js file:
Page({
data: {
sourcePath:'',
dstFilePath:'',
sourceName:''
},
chooseFile(e){
var self = this
wx.chooseMessageFile({
count: 1,
type:'file',
success(res){
const x = res.tempFiles[0].path
const y = res.tempFiles[0].name
console.log('选择',res)
self.setData({
sourcePath:x,
sourceName:y
})
}
})
},
uploadFile(){
var sourcePath = this.data.sourcePath
var sourceName = this.data.sourceName
self = this
wx.cloud.uploadFile({
cloudPath:'temp/'+sourceName, //这里的'temp/'是在环境中创建的文件夹
filePath: sourcePath
})
}
})