Home  >  Article  >  WeChat Applet  >  Detailed explanation of WeChat applet file API

Detailed explanation of WeChat applet file API

小云云
小云云Original
2018-05-30 14:22:2710503browse

This article mainly shares with you the detailed explanation of the WeChat applet file API, hoping to help everyone.

1. Little knowledge

1.wx.saveFile(OBJECT):Save the file locally.


#

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):Get the local saved file list


#

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

3.

wx.getSavedFileInfo(OBJECT):Get the file information of the local file


#

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

wx.removeSavedFile(OBJECT):

Delete locally stored files


#

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):Open the document in a new page, supported formats: 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('打开文档成功')
      }
    })
  }
})
二. Liezi

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:&#39;&#39;,
    	filesize:0,
 	},
  	upload:function(){
	    var that=this
	    wx.chooseImage({
	        count: 1, 
	        sizeType: [&#39;original&#39;, &#39;compressed&#39;],// 可以指定是原图还是压缩图,默认二者都有 
	        sourceType: [&#39;album&#39;, &#39;camera&#39;],  // 可以指定来源是相册还是相机,默认二者都有
	        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):打开文档

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

//获取应用实例
var app = getApp()
Page({
  	data:{
    	path:&#39;&#39;,
 	},
  	upload:function(){
	    var that=this
	    wx.downloadFile({
		  	url: &#39;http://192.168.56.1/sino-ui/www.941in.com.hk/m.v1/o.pptx&#39;,//文件的在本地的路径
		  	success: function (res) {
			    var filePath = res.tempFilePath
			    wx.openDocument({
			      	filePath: filePath,
			      	success: function (res) {
			        	console.log(&#39;打开文档成功&#39;)
			      	}
			    })
		  	}
		})
  	}
})

这个文件的路径,必须是http或是Https,不能使url: 'D:/WWW/sino-ui/www.941in.com.hk/m.v1/o.pptx',



相关推荐:

jQuery必须掌握的API

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

PHP关于API接口实例分享

The above is the detailed content of Detailed explanation of WeChat applet file API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn