微信小程序中如何上傳和下載文件


.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)
          }
          })
          },













    })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM