小程式文件整理之 -- API(檔案)
阿新 • • 發佈:2019-01-22
檔案
wx.saveFile(object)
儲存檔案到本地
wx.chooseImage({
success: function(res) {
var tempFilePaths = res.tempFilePaths
wx.saveFile({
tempFilePath: tempFilePaths[0],//需要儲存的檔案的臨時路徑
success: function(res) {//返回檔案的儲存路徑
var savedFilePath = res.savedFilePath//檔案的儲存路徑
},
fail/complete: function (res) {//介面呼叫失敗/結束的回撥函式
}
})
}
})
注意:本地檔案儲存的大小限制為 10M
wx.getFileInfo(object)
獲取本地已儲存的檔案列表
wx.getSavedFileList({
success: function(res) {
res.errMsg//介面呼叫結果
res.fileList//檔案列表
res.fileList.filePath//檔案的本地路徑
res.fileList.createTime//檔案的儲存時的時間戳,從1970/01/01 08:00:00 到當前時間的秒數
res.fileList.size//檔案大小,單位B
},
complete: function (res){//介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)
}
})
wx.getSavedFileList(object)
獲取本地檔案的檔案資訊。此介面只能用於獲取已儲存到本地的檔案,若需要獲取臨時檔案資訊,請使用 wx.getFileInfo 介面
wx.getSavedFileInfo({
filePath: 'wxfile://somefile', //檔案路徑
success: function(res) {
res.errMsg//介面呼叫結果
res.createTime//檔案的儲存時的時間戳,從1970/01/01 08:00:00 到當前時間的秒數
res.size//檔案大小,單位B
}
complete: function (res){//介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)
}
})
wx.getSavedFileInfo(object)
獲取本地檔案的檔案資訊。此介面只能用於獲取已儲存到本地的檔案,若需要獲取臨時檔案資訊,請使用 wx.getFileInfo 介面
wx.getSavedFileInfo({
filePath: 'wxfile://somefile', //僅做示例用,非真正的檔案路徑
success: function(res) {
res.errMsg//介面呼叫結果
res.createTime//檔案的儲存時的時間戳,從1970/01/01 08:00:00 到當前時間的秒數
res.size//檔案大小,單位B
}
complete: function (res){//介面呼叫結束的回撥函式(呼叫成功、失敗都會執行)
}
})
wx.removeSavedFile(object)
刪除本地儲存的檔案
wx.getSavedFileList({
success: function(res) {
if (res.fileList.length > 0){
//刪除本地儲存的檔案
wx.removeSavedFile({
filePath: res.fileList[0].filePath,//需要刪除的檔案路徑
success/fail/complete: function (res) {//介面呼叫成功/失敗/結束的回撥函式
}
})
}
}
})
wx.openDocument(object)
新開頁面開啟文件,支援格式:doc, xls, ppt, pdf, docx, xlsx, pptx
wx.downloadFile({
url: 'http://example.com/somefile.pdf',
success: function (res) {
var filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,//(必要)檔案路徑,可通過 downFile 獲得
fileType://檔案型別,指定檔案型別開啟檔案,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
success/fail/complete: function (res) {//介面呼叫成功/失敗/結束的回撥函式
}
})
}
})