一.小知識
1.wx.saveFile(OBJECT):保存(cun)文件到本地。
wx.chooseImage({
success: function(res) {
var tempFilePaths = res.tempFilePaths
wx.saveFile({
tempFilePath: tempFilePaths[0],
success: function(res) {
var savedFilePath = res.savedFilePath
}
})
}
})
2.wx.getSavedFileList(OBJECT):獲取本(ben)地(di)已保存(cun)的文件列表(biao)
wx.getSavedFileList({
success: function(res) {
console.log(res.fileList)
}
})
3.wx.getSavedFileInfo(OBJECT):獲取(qu)本地文件的文件信息
wx.getSavedFileInfo({
filePath: 'wxfile://somefile', //僅(jin)做示例用(yong),非(fei)真正的文件路徑(jing)
success: function(res) {
console.log(res.size)
console.log(res.createTime)
}
})
4.wx.removeSavedFile(OBJECT):刪(shan)除(chu)本地存儲(chu)的(de)文件
wx.getSavedFileList({
success: function(res) {
if (res.fileList.length > 0){
wx.removeSavedFile({
filePath: res.fileList[0].filePath,
complete: function(res) {
console.log(res)
}
})
}
}
})
5.wx.openDocument(OBJECT):新開頁面打開文檔,支(zhi)持(chi)格式:doc, xls, ppt, pdf, docx, xlsx, pptx
wx.downloadFile({
url: '//example.com/somefile.pdf',
success: function (res) {
var filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打開文檔成功')
}
})
}
})
二.列子
3.wx.getSavedFileInfo(OBJECT):獲取本地文件的(de)文件信息
<view class="container">
<button type="primary" bindtap="upload">上傳(chuan)文(wen)件(jian)</button>
<text>文(wen)件(jian)的路徑:{{ path}}px</text>
<text>文(wen)件(jian)大(da)小:{{filesize}}</text>
</view>
//獲取(qu)應(ying)用實例
var app = getApp()
Page({
data:{
path:'',
filesize:0,
},
upload:function(){
var that=this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],// 可以指(zhi)定是原圖(tu)還是壓(ya)縮圖(tu),默(mo)認二者都有(you)
sourceType: ['album', 'camera'], // 可以指(zhi)定來源是相冊還是相機,默(mo)認二者都有(you)
success: function (res) {
var tempFilePaths = res.tempFilePaths;
console.log(tempFilePaths)
wx.getSavedFileInfo({
filePath:res.tempFilePaths[0], //僅做(zuo)示例用,非真正的文(wen)件路徑
success: function(res) {
that.setData({
filesize:res.size,
})
}
})
that.setData({
path:tempFilePaths
})
}
})
}
})
5.wx.openDocument(OBJECT):打開文檔
<view class="container">
<button type="primary" bindtap="upload">打(da)開文件</button>
</view>
//獲取應用(yong)實(shi)例
var app = getApp()
Page({
data:{
path:'',
},
upload:function(){
var that=this
wx.downloadFile({
url: '//192.168.56.1/sino-ui/www.941in.com.hk/m.v1/o.pptx',//文(wen)件的(de)(de)在本地的(de)(de)路徑
success: function (res) {
var filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打開文(wen)檔成功')
}
})
}
})
}
})
這個文件的路徑,必須(xu)是(shi)http或是(shi)Https,不能使url: 'D:/WWW/sino-ui/www.941in.com.hk/m.v1/o.pptx',