微信小程序如何去下载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: '加载中...',

})
相关推荐
陈随易33 分钟前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级
前端·后端·程序员
爱丶狸1 小时前
Grafana_Zabbix_ImageRenderer_部署与前端操作手册
linux·前端·zabbix·grafana·kylin
用户059540174464 小时前
把AI对话记忆存储测试从手工改成Playwright+pytest,覆盖率从20%提到96%,回归时间缩短90%
前端·css
kyriewen4 小时前
别再这样写TypeScript了——Code Review中最常见的8个反模式
前端·javascript·typescript
名字还没想好☜4 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
午安~婉4 小时前
GitHub Token/ GitHub Stats统计显示图异常
前端·github·vercel·github token
码云之上5 小时前
AI Agent 工程化总览篇:从 Prompt 到 Harness
前端·人工智能
2501_926978335 小时前
以说明书 DNA 为模板——完整 AGI 的结构图景
前端·人工智能·经验分享·笔记·ai写作
IT_陈寒5 小时前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端
程序员黑豆6 小时前
鸿蒙应用开发:AttributeModifier 使用教程
前端·harmonyos