本文主要和大家分享微信小程序文件類API詳解,希望能幫助到大家。愛掏網 - it200.com
一.小知識
1.wx.saveFile(OBJECT):保存文件到本地。愛掏網 - it200.com
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):獲取本地已保存的文件列表
wx.getSavedFileList({ success: function(res) { console.log(res.fileList) } })登錄后復制
3.wx.getSavedFileInfo(OBJECT):獲取本地文件的文件信息
wx.getSavedFileInfo({ filePath: 'wxfile://somefile', //僅做示例用,非真正的文件路徑 success: function(res) { console.log(res.size) console.log(res.createTime) } })登錄后復制
4.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) } }) } } })登錄后復制
5.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, success: function (res) { console.log('打開文檔成功') } }) } })登錄后復制
二.列子
3.wx.getSavedFileInfo(OBJECT):獲取本地文件的文件信息
登錄后復制文件的路徑:{{ path}}px 文件大小:{{filesize}}
//獲取應用實例 var app = getApp() Page({ data:{ path:'', filesize:0, }, upload:function(){ var that=this wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'],// 可以指定是原圖還是壓縮圖,默認二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有 success: function (res) { var tempFilePaths = res.tempFilePaths; console.log(tempFilePaths) wx.getSavedFileInfo({ filePath:res.tempFilePaths[0], //僅做示例用,非真正的文件路徑 success: function(res) { that.setData({ filesize:res.size, }) } }) that.setData({ path:tempFilePaths }) } }) } })登錄后復制
5.wx.openDocument(OBJECT):打開文檔
登錄后復制
//獲取應用實例 var app = getApp() Page({ data:{ path:'', }, upload:function(){ var that=this wx.downloadFile({ url: 'http://192.168.56.1/sino-ui/www.941in.com.hk/m.v1/o.pptx',//文件的在本地的路徑 success: function (res) { var filePath = res.tempFilePath wx.openDocument({ filePath: filePath, success: function (res) { console.log('打開文檔成功') } }) } }) } })登錄后復制
這個文件的路徑,必須是http或是Https,不能使url: 'D:/WWW/sino-ui/www.941in.com.hk/m.v1/o.pptx',
相關推薦:
jQuery必須掌握的API
PHP如何開發api接口安全驗證實例
PHP關于API接口實例分享
以上就是微信小程序文件類API詳解的詳細內容,更多請關注愛掏網 - it200.com其它相關文章!
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。