页面使用示例
<button @click="shareFileFuc">转发</button>
import {shareFile} from '@/utils/index.js'
//分享
shareFileFuc(){
let text = '自定义名称';
shareFile(pdf地址,text)
},
utils/index.js内容
export const shareFile = (url,name) => {
console.log(url);
uni.downloadFile({
url: url,
success: (res) => {
console.log(res);
if (res.statusCode === 200) {
// 文件下载成功,获取临时文件路径
const tempFilePath = res.tempFilePath;
const fs = wx.getFileSystemManager();
const buffer = fs.readFileSync(tempFilePath, 'binary'); //读取文件
const customPath = `${wx.env.USER_DATA_PATH}/${name}.pdf`;
fs.writeFileSync(customPath, buffer, 'binary'); //写入文件
// 分享文件
wx.shareFileMessage({
filePath: customPath,
title: '分享的文件',
success: () => {
uni.showToast({
title: '分享成功',
icon: 'success'
});
},
fail: (err) => {
uni.showToast({
title: '分享失败',
icon: 'none'
});
console.error('分享失败:', err);
}
});
} else {
uni.showToast({
title: '文件下载失败',
icon: 'none'
});
}
},
fail: (err) => {
uni.showToast({
title: '下载失败',
icon: 'none'
});
console.error('下载失败:', err);
}
});
}