wx.saveFile(Object object)
saveFile 会把临时文件移动,因此调用成功后传入的 tempFilePath 将不可用,本地文件存储的大小限制为 10M
wx.chooseImage({
success: function(res) {
const tempFilePaths = res.tempFilePaths
wx.saveFile({
tempFilePath: tempFilePaths[0],
success (res) {
const savedFilePath = res.savedFilePath
}
})
}
})
FileSystemManager.saveFile(Object object)
保存临时文件到本地。此接口会移动临时文件,因此调用成功后,tempFilePath 将不可用。 此方法存储可以自己定义目录,可以实现需要分账号角色存储的功能。
save_photo: function () {
const fs = wx.getFileSystemManager()
var isExist = false;
var that = this;
try {
fs.accessSync(`${wx.env.USER_DATA_PATH}/photoinvoice/1553115`)
isExist = true;
} catch (e) {
}
if (isExist != true) {
try {
fs.mkdirSync(`${wx.env.USER_DATA_PATH}/photoinvoice/1553115`, true)
} catch (e) {
console.error(e)
}
}
console.log(that.data.src)
wx.getImageInfo({
src: this.data.src,
success: function (res) {
var path = res.path;
console.log("path:" + path)
var startPos = path.lastIndexOf("//");
console.log(startPos)
var picName = path.slice(startPos + 2, path.length);
console.log(picName)
fs.saveFile({
tempFilePath: path,
filePath: `${wx.env.USER_DATA_PATH}/photoinvoice/1553115/${picName}`,
success: function (res) {
console.log("保存成功")
console.log(res)
},
fail: function (res) {
console.log("保存失败")
console.log(res)
}
})
}
})
},
下载文件至本地,并打开文档
downloadfile(e){
var url = e.currentTarget.dataset.url;
wx.downloadFile({
url: url,
success(res) {
wx.saveFile({
tempFilePath: res.tempFilePath,
success: function (res) {
const savedFilePath = res.savedFilePath;
wx.openDocument({
filePath: savedFilePath,
success: function (res) {
console.log('打开文档成功')
},
});
},
fail: function (err) {
console.log('保存失败:', err)
}
});
}
})
},
|