跳到主要内容

文件工具方法

type FileEncodeing =
| "utf8"
| "ascii"
| "base64"
| "binary"
| "hex"
| "ucs2"
| "ucs-2"
| "utf16le"
| "utf-16le"
| "utf-8"
| "latin1"
| undefined;

type OpenFileFlag =
| "a+"
| "a"
| "ax"
| "ax+"
| "as"
| "as+"
| "r"
| "r+"
| "w"
| "wx"
| "w+"
| "wx+"
| undefined;

type FileType =
| "doc"
| "docx"
| "xls"
| "xlsx"
| "ppt"
| "pptx"
| "pdf"
| undefined;

const fileSystemManager = wx.getFileSystemManager();

export default {
// 将临时文件存储为本地文件(注意:saveFile 会把临时文件移动,因此调用成功后传入的 tempFilePath 将不可用)
saveTempFile(tempFilePath: string) {
return new Promise((resolve, reject) => {
fileSystemManager.saveFile({
tempFilePath: tempFilePath,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},

// 删除本地缓存文件
removeSavedFile(filePath: string) {
return new Promise((resolve, reject) => {
fileSystemManager.removeSavedFile({
filePath: filePath,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},

// 新页面打开文件
openDocumentInNewPage(filePath: string, fileType: FileType) {
return new Promise((resolve, reject) => {
wx.openDocument({
filePath: filePath,
fileType: fileType,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 打开文件(返回文件描述符)
openFile(filePath: string, flag: OpenFileFlag = "a+") {
return new Promise((resolve, reject) => {
fileSystemManager.open({
filePath: `${wx.env.USER_DATA_PATH}` + filePath,
flag: flag,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 关闭文件(fd:文件描述符)
closeFile(fd: any) {
return new Promise((resolve, reject) => {
fileSystemManager.close({
fd: fd,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},

// 获取本地文件的文件信息。(只能用于获取已保存到本地的文件)
// 会返回文件大小和文件保存时的时间戳,从1970/01/01 08:00:00 到该时刻的秒数
getSavedFileList() {
return new Promise((resolve, reject) => {
fileSystemManager.getSavedFileList({
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 获取文件信息(此接口可以获取临时文件的信息,但是返回的结果中不含时间)
// 只能返回文件大小和按照传入的 digestAlgorithm 计算得出的的文件摘要
getFileInfo(filePath: string) {
return new Promise((resolve, reject) => {
fileSystemManager.getFileInfo({
filePath: filePath,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 查看是否存在对应文件(此处默认在用户目录wx.env.USER_DATA_PATH下搜索)
hasFileOrPath(path: string) {
console.log("查看路径:" + `${wx.env.USER_DATA_PATH}` + path);
return new Promise((resolve, reject) => {
fileSystemManager.access({
path: `${wx.env.USER_DATA_PATH}` + path,
success(res) {
// 文件存在
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},

// 获取文件的状态信息
// (fd)文件描述符
fstatFile(fd: any) {
return new Promise((resolve, reject) => {
fileSystemManager.fstat({
fd: fd,
success(res) {
console.log(res);
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 读取目录下的文件列表
readDir(path: string) {
return new Promise((resolve, reject) => {
fileSystemManager.readdir({
dirPath: `${wx.env.USER_DATA_PATH}/` + path,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 读取文件内容
readFile(path: string, encoding: FileEncodeing = "utf8") {
const fs = wx.getFileSystemManager();
return new Promise((resolve, reject) => {
// 打开文件
fs.readFile({
filePath: `${wx.env.USER_DATA_PATH}` + path,
encoding: encoding,
position: 0,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 保存图片到本地相册
// filePath 图片文件路径,可以是临时文件路径或永久文件路径 (本地路径) ,不支持网络路径
saveImageToPhotosAlbum(filePath: string) {
return new Promise((resolve, reject) => {
wx.saveImageToPhotosAlbum({
filePath: filePath,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 保存视频到本地相册
saveVideoToPhotosAlbum(filePath: string) {
return new Promise((resolve, reject) => {
wx.saveVideoToPhotosAlbum({
filePath: filePath,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 创建目录
makeDir(path: string) {
return new Promise((resolve, reject) => {
fileSystemManager.mkdir({
dirPath: `${wx.env.USER_DATA_PATH}` + path,
// 默认如果父级目录不存在就报错
recursive: false,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},

// 追加文件内容
appendFile(path: string, data: any) {
return new Promise((resolve, reject) => {
fileSystemManager.open({
filePath: `${wx.env.USER_DATA_PATH}` + path,
flag: "a+",
success(res) {
// 写入文件
fileSystemManager.write({
fd: res.fd,
data: data,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
},
});
});
},

// 写文件
writeFile(path: string, data: any) {
return new Promise((resolve, reject) => {
fileSystemManager.open({
filePath: `${wx.env.USER_DATA_PATH}` + path,
flag: "w+",
success(res) {
// 写入文件
fileSystemManager.write({
fd: res.fd,
data: data,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
},
});
});
},
};