1. 程式人生 > >微信小程式API 檔案

微信小程式API 檔案

wx.saveFile(OBJECT)


儲存檔案到本地。

OBJECT引數說明:

引數 型別 必填 說明
tempFilePath String 需要儲存的檔案的臨時路徑
success Function 返回檔案的儲存路徑,res = {savedFilePath: '檔案的儲存路徑'}
fail Function 介面呼叫失敗的回撥函式
complete Function 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)

success返回引數說明:

引數 說明
savedFilePath 檔案的儲存路徑

示例程式碼:

wx.chooseImage({
  success: function(res) {
    var tempFilePath = res.tempFilePath
    wx.saveFile({
      tempFilePath: tempFilePath[0],
      success: function
(res)
{ var savedFilePath = res.savedFilePath } }) } })

bug & tip

  1. tip: 本地檔案儲存的大小限制為 10M

wx.getFileInfo(OBJECT)

基礎庫 1.4.0 開始支援,低版本需做相容處理

獲取檔案資訊

OBJECT引數說明:

引數名 型別 必填 說明
filePath String 本地檔案路徑
digestAlgorithm String 計算檔案摘要的演算法,預設值 md5,有效值:md5,sha1
success Function 介面呼叫成功的回撥函式
fail Function 介面呼叫失敗的回撥函式
complete Function 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)

success返回引數說明:

引數名 型別 說明
size Number 檔案大小,單位:B
digest String 按照傳入的 digestAlgorithm 計算得出的的檔案摘要
errMsg String 呼叫結果

示例程式碼:

wx.getFileInfo({
    success(res) {
        console.log(res.size)
        console.log(res.digest)
    }
})

wx.getSavedFileList(OBJECT)


獲取本地已儲存的檔案列表

OBJECT引數說明:

引數 型別 必填 說明
success Function 介面呼叫成功的回撥函式,返回結果見success返回引數說明
fail Function 介面呼叫失敗的回撥函式
complete Function 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)

success返回引數說明:

引數 型別 說明
errMsg String 介面呼叫結果
fileList Object Array 檔案列表

fileList中的專案說明:

型別 說明
filePath String 檔案的本地路徑
createTime Number 檔案的儲存時的時間戳,從1970/01/01 08:00:00 到當前時間的秒數
size Number 檔案大小,單位B

示例程式碼:

wx.getSavedFileList({
  success: function(res) {
    console.log(res.fileList)
  }
})

wx.getSavedFileInfo(OBJECT)

獲取本地檔案的檔案資訊。此介面只能用於獲取已儲存到本地的檔案,若需要獲取臨時檔案資訊,請使用 wx.getFileInfo 介面。

OBJECT引數說明:

引數 型別 必填 說明
filePath String 檔案路徑
success Function 介面呼叫成功的回撥函式,返回結果見success返回引數說明
fail Function 介面呼叫失敗的回撥函式
complete Function 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)

success返回引數說明:

引數 型別 說明
errMsg String 介面呼叫結果
size Number 檔案大小,單位B
createTime Number 檔案的儲存是的時間戳,從1970/01/01 08:00:00 到當前時間的秒數

示例程式碼:

wx.getSavedFileInfo({
  filePath: 'wxfile://somefile', //僅做示例用,非真正的檔案路徑
  success: function(res) {
    console.log(res.size)
    console.log(res.createTime)
  }
})

wx.removeSavedFile(OBJECT)

刪除本地儲存的檔案

OBJECT引數說明:

引數 型別 必填 說明
filePath String 需要刪除的檔案路徑
success Function 介面呼叫成功的回撥函式
fail Function 介面呼叫失敗的回撥函式
complete Function 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)

示例程式碼:

wx.getSavedFileList({
  success: function(res) {
    if (res.fileList.length > 0){
      wx.removeSavedFile({
        filePath: res.fileList[0].filePath,
        complete: function(res) {
          console.log(res)
        }
      })
    }
  }
})

wx.openDocument(OBJECT)

新開頁面開啟文件,支援格式:doc, xls, ppt, pdf, docx, xlsx, pptx

OBJECT引數說明:

引數 說明 必填 說明 最低版本
filePath String 檔案路徑,可通過 downFile 獲得  
fileType String 檔案型別,指定檔案型別開啟檔案,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx 1.4.0
success Function 介面呼叫成功的回撥函式  
fail Function 介面呼叫失敗的回撥函式  
complete Function 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)  

示例程式碼

wx.downloadFile({  url: 'http://example.com/somefile.pdf',
  success: function (res) {
    var filePath = res.tempFilePath 
    wx.openDocument({
      filePath: filePath,
      success: function (res) {
        console.log('開啟文件成功')
      }
    })
  }
})