当前位置:首页站长学院后端开发微信小程序文件类API详解
企业营销,就选知企PROSAAS

微信小程序文件类API详解

本文主要和大家分享微信小程序文件类API详解,希望能帮助到大家。

一.小知识

1.wx.saveFile(OBJECT):保存文件到本地。

微信小程序文件类API详解

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):获取本地已保存的文件列表

微信小程序文件类API详解

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

3.wx.getSavedFileInfo(OBJECT):获取本地文件的文件信息

微信小程序文件类API详解

wx.getSavedFileInfo({
  filePath: 'wxfile://somefile', //仅做示例用,非真正的文件路径
  success: function(res) {
    console.log(res.size)
    console.log(res.createTime)
  }
})

4.wx.removeSavedFile(OBJECT):删除本地存储的文件

微信小程序文件类API详解

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

微信小程序文件类API详解

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):获取本地文件的文件信息

<view class="container">
	<button type="primary" bindtap="upload">上传文件</button>
	<text>文件的路径:{{ path}}px</text>
	<text>文件大小:{{filesize}}</text>
</view>
//获取应用实例
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
	            })  
	        }
	    })
  	}
})

微信小程序文件类API详解

5.wx.openDocument(OBJECT):打开文档

<view class="container">
	<button type="primary" bindtap="upload">打开文件</button>
</view>

//获取应用实例
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’,

微信小程序文件类API详解

相关推荐:

jQuery必须掌握的API

PHP如何开发api接口安全验证实例

PHP关于API接口实例分享

以上就是微信小程序文件类API详解的详细内容,更多请关注知企PROSAAS其它相关文章!

温馨提示:

文章标题:微信小程序文件类API详解

文章链接:https://ceshi.prosaas.cn/17530.html

更新时间:2018年05月30日

声明: 本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:973664285@qq.com我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读知企PROSAAS协议

给TA打赏
共{{data.count}}人
人已打赏
后端开发

最简单的微信小程序Demo

2018-5-30 10:20:06

后端开发

如何使用微信小程序做出图片上传

2018-5-31 14:10:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索