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

})
相关推荐
KaMeidebaby1 小时前
卡梅德生物技术快报|细菌 FISH 实验 + 流式细胞术:尿路感染活菌快速定量系统实现与数据验证
前端·数据库·其他·百度·新浪微博
昆曲之源_娄江河畔1 小时前
DBGridEh Footer的使用
前端·数据库·delphi·dbgrideh
廖松洋(Alina)1 小时前
02数据模型与单词仓库-鸿蒙PC端Electron开发
前端·华为·electron·开源·harmonyos·鸿蒙
幽络源小助理1 小时前
最新短网址系统源码 分用户链接 - 幽络源免费源码分享
前端·php
Muen1 小时前
SwiftUI-学习路线
前端
小小小小宇2 小时前
普通 H5 新版本部署后通知用户更新方案
前端
zithern_juejin2 小时前
JS深拷贝与浅拷贝
javascript
小陈工2 小时前
Python异步编程进阶:asyncio高级模式与性能调优
开发语言·前端·数据库·人工智能·python·flask·numpy
小小小小宇2 小时前
App 内嵌 H5 秒开技术方案
前端