天天看點

小程式中檔案相關api總結

wx.saveFile(OBJECT)

儲存檔案到本地。注意:saveFile 會把臨時檔案移動,是以調用成功後傳入的 tempFilePath 将不可用

OBJECT參數說明:

參數名 類型 必填 說明
count Number 最多可以選擇的圖檔張數,預設9
sizeType StringArray original 原圖,compressed 壓縮圖,預設二者都有
sourceType album 從相冊選圖,camera 使用相機,預設二者都有
success Function 成功則傳回圖檔的本地檔案路徑清單 tempFilePaths
fail 接口調用失敗的回調函數
complete 接口調用結束的回調函數(調用成功、失敗都會執行)

success傳回參數說明:

參數
savedFilePath 檔案的儲存路徑
wx.chooseImage({
  success: function(res) {
    var tempFilePaths = res.tempFilePaths
    wx.saveFile({
      tempFilePath: tempFilePaths[0],
      success: function(res) {
        var savedFilePath = res.savedFilePath
      }
    })
  }
})           

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

wx.getSavedFileList(OBJECT)

擷取本地已儲存的檔案清單

接口調用成功的回調函數,傳回結果見success傳回參數說明
errMsg String 接口調用結果
fileList Object Array 檔案清單

fileList中的項目說明:

filePath 檔案的本地路徑
createTime 檔案的儲存時的時間戳,從1970/01/01 08:00:00 到目前時間的秒數
size 檔案大小,機關B
wx.getSavedFileList({
  success: function(res) {
    console.log(res.fileList)
  }
})           

wx.getSavedFileInfo(OBJECT)

擷取本地檔案的檔案資訊。此接口隻能用于擷取已儲存到本地的檔案,若需要擷取臨時檔案資訊,請使用 wx.getFileInfo 接口

檔案路徑
檔案儲存時的時間戳,從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)

删除本地存儲的檔案

需要删除的檔案路徑
接口調用成功的回調函數
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

最低版本
檔案路徑,可通過 downFile 獲得
fileType 檔案類型,指定檔案類型打開檔案,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx 1.4.0
wx.downloadFile({
  url: 'http://example.com/somefile.pdf',
  success: function (res) {
    var filePath = res.tempFilePath
    wx.openDocument({
      filePath: filePath,
      success: function (res) {
        console.log('打開文檔成功')
      }
    })
  }
})           

檔案管理器

FileSystemManager:檔案管理器。基礎庫 1.9.9 開始支援,低版本需做相容處理

方法

FileSystemManager.access(Object object):判斷檔案/目錄是否存在

FileSystemManager.appendFile(Object object):在檔案結尾追加内容

FileSystemManager.saveFile(Object object):儲存臨時檔案到本地。此接口會移動臨時檔案,是以調用成功後,tempFilePath 将不可用。

FileSystemManager.getSavedFileList():擷取該小程式下已儲存的本地緩存檔案清單

FileSystemManager.removeSavedFile(Object object):删除該小程式下已儲存的本地緩存檔案

FileSystemManager.copyFile(Object object):複制檔案

FileSystemManager.getFileInfo(Object object):擷取該小程式下的 本地臨時檔案 或 本地緩存檔案 資訊

FileSystemManager.mkdir(Object object):建立目錄

FileSystemManager.readdir(Object object):讀取目錄内檔案清單

FileSystemManager.readFile(Object object):讀取本地檔案内容

FileSystemManager.rename(Object object):重命名檔案,可以把檔案從 oldPath 移動到 newPath

FileSystemManager.rmdir(Object object):删除目錄

Stats FileSystemManager.stat(Object object):擷取檔案 Stats 對象

FileSystemManager.unlink(Object object):删除檔案

FileSystemManager.unzip(Object object):解壓檔案

FileSystemManager.writeFile(Object object):寫檔案

FileSystemManager wx.getFileSystemManager()

擷取全局唯一的檔案管理器。基礎庫 1.9.9 開始支援,低版本需做相容處理

傳回值

FileSystemManager:檔案管理器

FileSystemManager.appendFile(Object object)

在檔案結尾追加内容。基礎庫 2.1.0 開始支援,低版本需做相容處理

屬性 預設值 支援版本
要追加内容的檔案路徑
data string/ArrayBuffer 要追加的文本或二進制資料
encoding string utf8
指定寫入檔案的字元編碼

object.encoding 的合法值

ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序讀取
utf-8/utf8
latin1

fail 回調函數

錯誤資訊

res.errMsg 的合法值

fail no such file or directory, open ${filePath} 指定的 filePath 檔案不存在
fail illegal operation on a directory, open "${filePath}" 指定的 filePath 是一個已經存在的目錄
fail permission denied, open ${dirPath} 指定的 filePath 路徑沒有寫權限
fail sdcard not mounted

FileSystemManager.access(Object object)

判斷檔案/目錄是否存在。基礎庫 1.9.9 開始支援,低版本需做相容處理

path 要判斷是否存在的檔案/目錄路徑
fail no such file or directory ${path} 檔案/目錄不存在

FileSystemManager.accessSync(string path)

FileSystemManager.access 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string path:要判斷是否存在的檔案/目錄路徑

錯誤

FileSystemManager.appendFileSync(string filePath, string|ArrayBuffer data, string encoding)

FileSystemManager.appendFile 的同步版本。基礎庫 2.1.0 開始支援,低版本需做相容處理

string filePath:要追加内容的檔案路徑

string|ArrayBuffer data:要追加的文本或二進制資料

string encoding:指定寫入檔案的字元編碼

encoding 的合法值

FileSystemManager.copyFile(Object object)

複制檔案。基礎庫 1.9.9 開始支援,低版本需做相容處理

srcPath 源檔案路徑,隻可以是普通檔案
destPath 目标檔案路徑
fail permission denied, copyFile ${srcPath} -> ${destPath} 指定目标檔案路徑沒有寫權限
fail no such file or directory, copyFile ${srcPath} -> ${destPath} 源檔案不存在,或目标檔案路徑的上層目錄不存在

FileSystemManager.copyFileSync(string srcPath, string destPath)

FileSystemManager.copyFile 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string srcPath:源檔案路徑,隻可以是普通檔案

string destPath:目标檔案路徑

FileSystemManager.getSavedFileList(Object object)

擷取該小程式下已儲存的本地緩存檔案清單。基礎庫 1.9.9 開始支援,低版本需做相容處理

success 回調函數

Object 檔案數組,每一項是一個 FileItem

res.fileList 的結構

本地路徑
number 本地檔案大小,以位元組為機關
檔案建立時間

FileSystemManager.getFileInfo(Object object)

擷取該小程式下的 本地臨時檔案 或 本地緩存檔案 資訊。基礎庫 1.9.9 開始支援,低版本需做相容處理

要讀取的檔案路徑
檔案大小,以位元組為機關
fail file not exist 指定的 filePath 找不到檔案

FileSystemManager.mkdir(Object object)

建立目錄。基礎庫 1.9.9 開始支援,低版本需做相容處理

dirPath 建立的目錄路徑
fail no such file or directory ${dirPath} 上級目錄不存在
fail file already exists ${dirPath} 有同名檔案或目錄

FileSystemManager.mkdirSync(string dirPath)

FileSystemManager.mkdir 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string dirPath:建立的目錄路徑

FileSystemManager.removeSavedFile(Object object)

删除該小程式下已儲存的本地緩存檔案。基礎庫 1.9.9 開始支援,低版本需做相容處理

(fail file not exist) 指定的 tempFilePath 找不到檔案

string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding)

FileSystemManager.readFile 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string filePath:要讀取的檔案的路徑

string encoding:指定讀取檔案的字元編碼,如果不傳 encoding,則以 ArrayBuffer 格式讀取檔案的二進制内容

以小程式讀取

string|ArrayBuffer data:檔案内容

指定的 filePath 所在目錄不存在
指定的 filePath 路徑沒有讀權限

FileSystemManager.renameSync(string oldPath, string newPath)

FileSystemManager.rename 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string oldPath:源檔案路徑,可以是普通檔案或目錄

string newPath:新檔案路徑

fail permission denied, rename ${oldPath} -> ${newPath} 指定源檔案或目标檔案沒有寫權限
fail no such file or directory, rename ${oldPath} -> ${newPath}

FileSystemManager.rmdirSync(string dirPath)

FileSystemManager.rmdir 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string dirPath:要删除的目錄路徑

目錄不存在
fail directory not empty 目錄不為空
指定的 dirPath 路徑沒有寫權限

FileSystemManager.readdir(Object object)

讀取目錄内檔案清單。基礎庫 1.9.9 開始支援,低版本需做相容處理

要讀取的目錄路徑
files Array. 指定目錄下的檔案名數組
fail not a directory ${dirPath} dirPath 不是目錄

FileSystemManager.rename(Object object)

重命名檔案,可以把檔案從 oldPath 移動到 newPath。基礎庫 1.9.9 開始支援,低版本需做相容處理

oldPath 源檔案路徑,可以是普通檔案或目錄
newPath 新檔案路徑

FileSystemManager.readFile(Object object)

讀取本地檔案内容。基礎庫 1.9.9 開始支援,低版本需做相容處理

要讀取的檔案的路徑
指定讀取檔案的字元編碼,如果不傳 encoding,則以 ArrayBuffer 格式讀取檔案的二進制内容
檔案内容

FileSystemManager.rmdir(Object object)

删除目錄。基礎庫 1.9.9 開始支援,低版本需做相容處理

要删除的目錄路徑

Array. FileSystemManager.readdirSync(string dirPath)

FileSystemManager.readdir 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string dirPath:要讀取的目錄路徑

Array. files:指定目錄下的檔案名數組

FileSystemManager.saveFile(Object object)

儲存臨時檔案到本地。此接口會移動臨時檔案,是以調用成功後,tempFilePath 将不可用。基礎庫 1.9.9 開始支援,低版本需做相容處理

tempFilePath 臨時存儲檔案路徑
要存儲的檔案路徑
存儲後的檔案路徑
fail tempFilePath file not exist
fail permission denied, open "${filePath}"
fail no such file or directory "${dirPath}"

number FileSystemManager.saveFileSync(string tempFilePath, string filePath)

FileSystemManager.saveFile 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string tempFilePath:臨時存儲檔案路徑

string filePath:要存儲的檔案路徑

number savedFilePath:存儲後的檔案路徑

Stats FileSystemManager.stat(Object object)

擷取檔案 Stats 對象。基礎庫 1.9.9 開始支援,低版本需做相容處理

檔案/目錄路徑
stat Stats 一個 Stats 對象
fail permission denied, open ${path} 指定的 path 路徑沒有讀權限
檔案不存在

Stats FileSystemManager.statSync(string path)

FileSystemManager.stat 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string path:檔案/目錄路徑

Stats stat:一個 Stats 對象

描述檔案狀态的對象。基礎庫 1.9.9 開始支援,低版本需做相容處理

string mode:檔案的類型和存取的權限,對應 POSIX stat.st_mode

number size:檔案大小,機關:B,對應 POSIX stat.st_size

number lastAccessedTime:檔案最近一次被存取或被執行的時間,UNIX 時間戳,對應 POSIX stat.st_atime

number lastModifiedTime:檔案最後一次被修改的時間,UNIX 時間戳,對應 POSIX stat.st_mtime

boolean Stats.isDirectory():判斷目前檔案是否一個目錄

boolean Stats.isFile():判斷目前檔案是否一個普通檔案

boolean Stats.isDirectory()

判斷目前檔案是否一個目錄。基礎庫 1.9.9 開始支援,低版本需做相容處理

boolean:表示目前檔案是否一個目錄

boolean Stats.isFile()

判斷目前檔案是否一個普通檔案。基礎庫 1.9.9 開始支援,低版本需做相容處理

boolean:表示目前檔案是否一個普通檔案

FileSystemManager.unlink(Object object)

删除檔案。基礎庫 1.9.9 開始支援,低版本需做相容處理。

要删除的檔案路徑
fail operation not permitted, unlink ${filePath} 傳入的 filePath 是一個目錄

FileSystemManager.unzip(Object object)

解壓檔案。基礎庫 1.9.9 開始支援,低版本需做相容處理

zipFilePath 源檔案路徑,隻可以是 zip 壓縮檔案
targetPath 目标目錄路徑
fail permission denied, unzip ${zipFilePath} -> ${destPath}
fail no such file or directory, unzip ${zipFilePath} -> "${destPath}

FileSystemManager.unlinkSync(string filePath)

FileSystemManager.unlink 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string filePath:要删除的檔案路徑

FileSystemManager.writeFile(Object object)

寫檔案。基礎庫 1.9.9 開始支援,低版本需做相容處理

要寫入的檔案路徑
要寫入的文本或二進制資料

FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding)

FileSystemManager.writeFile 的同步版本。基礎庫 1.9.9 開始支援,低版本需做相容處理

string filePath:要寫入的檔案路徑

string|ArrayBuffer data:要寫入的文本或二進制資料