微信小程序文件类API的示例分析

发布时间:2021-06-04 13:56:12 作者:小新
来源:亿速云 阅读:299

这篇文章主要介绍微信小程序文件类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的示例分析

以上是“微信小程序文件类API的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. JS中promise化微信小程序api的示例分析
  2. 微信小程序个人开发的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

api 小程序

上一篇:mybatis属性的示例分析

下一篇:ES6中对象新功能与解构赋值的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》