通过预览文件用户手动操作分享保存实现分享功能。
deletContract() {
try {
let file = wx.getFileSystemManager();
file.readdir({
dirPath: `${wx.env.USER_DATA_PATH}`,
success: res => {
console.log(res);
if (res.files.length > 2) {
file.unlink({
filePath: `${wx.env.USER_DATA_PATH}/${res.files[0]}`,
complete: res => {
}
})
}
}
})
} catch (error) {
}
},
shareBg() {
let that = this;
const fileExtName = ".pdf";
const randfile ='报告单' + new Date().getTime() + fileExtName; //分享的文件名
const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
that.deletContract();
wx.downloadFile({
url: 'https://www.leomay.com/upload/file/mmo-20170707165001.pdf',//网络报告地址(接口请求回的)
filePath: newPath,
success: function(res) {
const filePath = res.tempFilePath;
wx.openDocument({
filePath: newPath,
showMenu: true,
fileType: 'pdf',
success: function(res) {}
})
},
fail: function(res) {
wx.hideLoading();
}
})
}
通过分享api直接分享到用户,无文件预览效果
deletContract() {
try {
let file = wx.getFileSystemManager();
file.readdir({
dirPath: `${wx.env.USER_DATA_PATH}`,
success: res => {
console.log(res);
if (res.files.length > 2) {
file.unlink({
filePath: `${wx.env.USER_DATA_PATH}/${res.files[0]}`,
complete: res => {
}
})
}
}
})
} catch (error) {
}
},
shareBg() {
let that = this;
const fileExtName = ".pdf";
const randfile ='报告单' + new Date().getTime() + fileExtName; //分享的文件名
const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
that.deletContract();
wx.downloadFile({
url: 'https://www.leomay.com/upload/file/mmo-20170707165001.pdf',
filePath: newPath,
success: function(res) {
const filePath = res.tempFilePath;
// wx.openDocument({
// filePath: newPath,
// showMenu: true,
// fileType: 'pdf',
// success: function(res) {}
// })
wx.shareFileMessage({
filePath: newPath,
success() {},
fail: console.error,
})
},
fail: function(res) {
wx.hideLoading();
}
})
}