.wxml
<button bindtap="chooseFile">選擇文件</button> <view>請輸入下載鏈接</view> <input bindinput="getContent"></input> <button bindtap="downLoad">下載</button>
.js
Page({ chooseFile(){ //上傳文件 var that=this wx.chooseMessageFile({ count: 1, type: 'all', success (res) { // tempFilePath可以作為img標簽的src屬性顯示圖片 const tempFilePaths = res.tempFiles console.log(tempFilePaths[0]) that.upload(tempFilePaths[0].path,tempFilePaths[0].name) } }) }, upload(tmpFile,updFile){ //修改 wx.cloud.uploadFile({ cloudPath: updFile, //修改 filePath: tmpFile, // 文件路徑 success: res => { // get resource ID console.log("上傳成功",res) }, fail: err => { // handle error console.log("上傳失敗",err) } }) }, //下載並打開文件 getContent(e){ console.log(e.detail.value) this.setData({ fileID:e.detail.value }) }, downLoad(){ var fileID fileID=this.data.fileID console.log("下載鏈接為:",fileID) wx.cloud.downloadFile({ fileID: fileID, success: res => { // get temp file path console.log("下載成功",res) const filePath = res.tempFilePath //新增 wx.openDocument({ //新增加 filePath: filePath, //新增加 success: function (res) { //新增加 console.log('打開文檔成功') //新增加 } }) }, fail: err => { // handle error console.log("下載失敗",res) } }) }, })