
image.png
const fs = wx.getSystemFileManager();可以獲取到全局唯一的文件系統(tǒng)管理器,所有文件系統(tǒng)的管理操作通過 FileSystemManager 來調(diào)用
1:創(chuàng)建文件前
fs.writeFile,必須要先創(chuàng)建目錄fs.mkdir和檢查是否存在fs.access2:在當(dāng)前文件中追加數(shù)據(jù)
fs.appendFile3:刪除當(dāng)前目錄中所有文件 讀取目錄中所有文件
fs.readdir--->循環(huán)files.forEach--->刪除fs.unlinkwx.env.USER_DATA_PATH本地文件的文件路徑均為以下格式:{{協(xié)議名}}://文件路徑
其中,協(xié)議名在 iOS/Android 客戶端為 "wxfile",在開發(fā)者工具上為 "http",開發(fā)者無需關(guān)注這個(gè)差異,也不應(yīng)在代碼中去硬編碼完整文件路徑。
注意:filePath一定要和你存儲(chǔ)的路徑一模一樣才可以刪除成功!不然會(huì)報(bào)沒有刪除權(quán)限errMsg:"unlink:fail fail permission denied, open "http://usr/data1800330233101.doc""(這個(gè)小程序的報(bào)錯(cuò)提示也是真的坑.)
http://usr/data1800330233101.doc
需要改成
http://usr/data/1800330233101.doc
wxml
<button bindtap="clickHandler" data-operation="{{1}}" type="default" style="margin-top: 10rpx;">創(chuàng)建目錄</button>
<button bindtap="clickHandler" data-operation="{{2}}" type="default" style="margin-top: 10rpx;">寫入文件</button>
<button bindtap="clickHandler" data-operation="{{3}}" type="default" style="margin-top: 10rpx;">文件是否存在</button>
<button bindtap="clickHandler" data-operation="{{4}}" type="default" style="margin-top: 10rpx;">保存文件</button>
<button bindtap="clickHandler" data-operation="{{5}}" type="default" style="margin-top: 10rpx;">打開文件</button>
<button bindtap="clickHandler" data-operation="{{6}}" type="default" style="margin-top: 10rpx;">繼續(xù)寫入</button>
<button bindtap="clickHandler" data-operation="{{7}}" type="default" style="margin-top: 10rpx;">刪除文件</button>
<button bindtap="clickHandler" data-operation="{{8}}" type="default" style="margin-top: 10rpx;">上傳文件</button>
js
let fs = null;
Page({
data: {
filePath: '', //文件地址
tempDatas: [
[
10, 11, 12, 13, 14, 15, 16, 17, '123456789'
]
]
},
onLoad(options) {
fs = wx.getFileSystemManager();
},
onShow() {},
clickHandler(e) {
const operation = e.currentTarget.dataset.operation;
switch (operation) {
case 1: //創(chuàng)建目錄
const _filePath = `${wx.env.USER_DATA_PATH}/data`;
fs.mkdir({
dirPath: _filePath,
recursive: false,
success: (res) => {
console.log(res);
},
fail: (err) => {
console.log(err);
},
});
break;
case 2: //寫入文件
const filePath_ = `${wx.env.USER_DATA_PATH}/data/` + Date.now() + '.doc';
this.data.filePath = filePath_;
fs.writeFile({
filePath: filePath_,
data: '1234567890',
encoding: 'utf8',
success: (res) => {
console.log(res);
},
fail: (err) => {
console.log(err);
},
});
break;
case 3: //文件是否存在
fs.access({
path: this.data.filePath,
success(res) {
console.log(res);
},
fail(err) {
console.log(err);
}
})
break;
case 4: //保存文件
fs.saveFile({
tempFilePath: this.data.filePath,
success: (result) => {
console.log(result);
},
fail: (res) => {
console.log(res);
},
})
break;
case 5: //打開文件
wx.openDocument({
filePath: this.data.filePath,
fileType: 'doc',
success: (res) => {
console.log(res);
},
fail: (err) => {
console.log(err);
}
})
break;
case 6: //繼續(xù)寫入
fs.appendFile({
filePath: this.data.filePath,
data: '\n' + this.data.tempDatas + '',
encoding: 'utf8',
success: (res) => {
console.log(res);
},
fail: (err) => {
console.log(err);
},
});
break;
case 7: //刪除 `${wx.env.USER_DATA_PATH}/data/`文件夾下所有文件
console.log(this.data.filePath);
fs.readdir({
dirPath: wx.env.USER_DATA_PATH,
success: (res) => {
console.log('readdir', res);
res.files.forEach((el) => {
fs.unlink({
filePath: `${wx.env.USER_DATA_PATH}/${el}`.replace(/data/g, "data/"),
success: (res) => {
console.log('unlink',res);
},
fail: (err) => {
console.log('unlink', err);
}
})
})
},
fail: (err) => {
console.log('readdir',err);
}
})
break;
case 8: //上傳文件(云服務(wù)接口)
wx.showLoading({
title: '請(qǐng)稍后',
});
const _that = this;
const name = this.data.filePath.substr(this.data.filePath.lastIndexOf('/') + 1)
wx.cloud.uploadFile({
cloudPath: 'data/' + name, // 指定上傳到的云路徑
filePath: _that.data.filePath,
}).then(res => {
console.log('上傳成功-云存儲(chǔ)', res);
wx.hideLoading();
wx.showToast({
title: '上傳成功',
})
}).catch(err => {
console.log('上傳失敗', err);
wx.hideLoading();
wx.showToast({
title: '上傳失敗',
})
});
break;
}
}
})