微信小程序如何去下载PDF呢

在Ai代码时代 我一开始以为这种下载非常简单的事情 ,在询问多次无果 后,决定还是看文档吧 ``

js 复制代码
// 点击手册项 - 预览和下载PDF

function onManualTap(item: any) {

if (!item.zd_down) {

uni.showToast({ title: '暂无文档', icon: 'none' })

return

}

  


// 拼接完整PDF链接

const baseUrl = getBaseUrl()

const pdfUrl = baseUrl + item.zd_down

uni.showLoading({ title: '预览下载', mask: true })

// 清理文件名中的特殊字符

const safeName = item.title.replace(/[\/\\:*?"<>|]/g, '_')

const filePath = `${wx.env.USER_DATA_PATH}/${safeName}.pdf`

  


uni.downloadFile({

url: pdfUrl,

success: (data) => {

if (data.statusCode === 200) {

uni.getFileSystemManager().saveFile({

tempFilePath: data.tempFilePath,

filePath: filePath,

success: (res1) => {

uni.hideLoading()

uni.openDocument({

filePath: res1.savedFilePath,

fileType: 'pdf',

showMenu: true,

fail: () => {

uni.showToast({ icon: 'none', title: '打开失败' })

},

})

},

fail: () => {

uni.hideLoading()

uni.showToast({ icon: 'none', title: '保存失败' })

},

})

} else {

uni.hideLoading()

}

},

fail: (err) => {

uni.hideLoading()

console.error('网络请求异常:', err)

if (err.errMsg?.includes('url not in domain')) {

uni.showModal({

title: '配置错误',

content: '请在小程序后台将域名加入 downloadFile 合法域名列表',

})

} else {

uni.showToast({ icon: 'none', title: '下载失败,请重试' })

}

},

})

}

这样 然后我就想到了 写一个方法 后续说不定能够用上呢,于是就有了

js 复制代码
/**

* 通用文件下载并打开方法

* @param options 下载配置

*/

export function downloadAndOpen(options: DownloadOptions): void {

const {

url,

fileName,

fileType = 'pdf',

showMenu = true,

loadingTitle = '加载中...',

} = options

  


// 拼接完整文件链接

const baseUrl = getBaseUrl()

const fullUrl = url.startsWith('http') ? url : baseUrl + url

  


// 清理文件名中的特殊字符

const safeName = fileName.replace(/[/\\:*?"<>|]/g, '_')

const filePath = `${wx.env.USER_DATA_PATH}/${safeName}.${fileType}`

  


uni.showLoading({ title: loadingTitle, mask: true })

  


uni.downloadFile({

url: fullUrl,

success: (data) => {

if (data.statusCode === 200) {

uni.getFileSystemManager().saveFile({

tempFilePath: data.tempFilePath,

filePath,

success: (res) => {

uni.hideLoading()

uni.openDocument({

filePath: res.savedFilePath,

fileType,

showMenu,

fail: () => {

uni.showToast({ icon: 'none', title: '打开失败' })

},

})

},

fail: () => {

uni.hideLoading()

uni.showToast({ icon: 'none', title: '保存失败' })

},

})

}

else {

uni.hideLoading()

uni.showToast({ icon: 'none', title: '下载失败' })

}

},

fail: (err) => {

uni.hideLoading()

console.error('下载异常:', err)

if (err.errMsg?.includes('url not in domain')) {

uni.showModal({

title: '配置错误',

content: '请在小程序后台将域名加入 downloadFile 合法域名列表',

})

}

else {

uni.showToast({ icon: 'none', title: '下载失败,请重试' })

}

},

})

}

案例

js 复制代码
downloadAndOpen({

url: item.zd_down,

fileName: item.title,

fileType: 'pdf',

loadingTitle: '加载中...',

})
相关推荐
李伟_Li慢慢16 分钟前
01-threejs架构原理-课程简介
前端·three.js
_瑞2 小时前
深入理解 iOS 渲染原理
前端·ios
IT_陈寒2 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端
iCOD3R2 小时前
Skill - kill-ai-slop 解决“AI 味”样式
前端·css·ai编程
shuaijie05182 小时前
强制修改调用接口的api地址。
javascript·vue.js·ecmascript
JerrySir3 小时前
恶意 npm 包只活了 17 分钟:技术面试里,为什么“升级依赖”还不算止血?
javascript·安全
皮音3 小时前
Skill 在项目中的实践 —— 从 Agent 困境到 Skill 工程化
前端
白帽小阳3 小时前
2026前端面试题!(附答案及解析)
javascript·网络·python·安全·web安全·网络安全·护网行动
大龄秃头程序员3 小时前
RN 0.86 新架构实战:TurboModule + Fabric 组件 + iOS 原生容器,完整代码开源
前端·react native
这是个栗子3 小时前
前端开发中的常用工具函数(八)
开发语言·前端·javascript