本文主要是介绍《微信小程序开发从入门到实战》学习七十五,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
6.8 文件API
6.8.4 删除文件API
使用wx.removeSavedFile可删除小程序中保存的本地文件。示例代码如下:
// 获取文件列表
wx.getSavedFileList({
success(res) {
if (res.fileList.length > 0) {
// 删除第一个文件
wx.removeSavedFile({
filePath: res.fileList[0].filePath,
complete(res) {
console.log(res)
}
})
}
}
})
6.8.5 文件信息API
使用wx.getSavedFileInfo可获取已保存的小程序本地的文件的信息,不能用于临时文件。示例代码如下:
wx.getSavedFileInfo({
filePath: someFilePath,
success(res) {
console.log(res.size) // 文件大小
console.log(res.creaTime) // 文件保存的时间
}
})
使用wx.getFileInfo可获取临时文件信息。示例代码如下:
wx.getFileInfo({
filePath: someFilePath,
digestAlgorithm: 'md5', // 计算文件摘要的算法,默认为md5,其他可选值为sha1
success(res) {
console.log(res.size) // 文件大小
console.log(res.digest) // 根据摘要算法计算出来的文件摘要
}
})
6.8.6 打开文档API
使用wx.openDocument可在小程序中打开文档文件。支持文件格式:doc、docx、xls、xlsx、ppt、pptx和pdf。示例代码如下:
wx.downloadFile({
url: 'http://example.com/somefile.pdf',
success: function(res) {
const filePath = res.tempFilePath
wx.openDocument({
filePath: 'filePath',
success: function(res) {
console.log('打开文档成功')
}
})
}
})
这篇关于《微信小程序开发从入门到实战》学习七十五的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!