微信小程序文件实现下载并预览(ios及安卓手机)

时间:2024-09-29 20:46:29

目录

1. 配置downloadFile域名

 2.  下载文件-代码实现以及注意事项

3. 微信小程序下载文件ios无法预览-完整方案


微信小程序上传文件可查看这篇文章:微信小程序 上传文件response的内容返回格式需要转变为json对象 

1. 配置downloadFile域名

登录小程序平台,找到【开发管理】-【开发设置】,在服务器域名模块设置downloadFile域名

 2.  下载文件-代码实现以及注意事项

 注意事项:

  • 后端返回的下载url地址必须是可预览的,不然无法在手机上查看预览 
  • ,注意fileType属性的值 
  • 方法 本地文件存储的大小限制为 10M,所以在下载之前先将本地缓存的文件一一删除

:

  1. <view class="cell-list-wrap">
  2. <view class="cell-list-file" wx:for="{{lists}}" wx:key="index">
  3. <text class="title">{{}}</text>
  4. <text class="down topZindex" bindtap="onDown" data-fileurl="{{}}">下载</text>
  5. </view>
  6. </view>

  1. // 下载
  2. Component({
  3. properties: {
  4. lists: {
  5. type: Array,
  6. value: []
  7. }
  8. },
  9. data: {},
  10. ready: function() { },
  11. methods: {
  12. // 下载文件
  13. /**
  14. * 本地文件存储的大小限制为 10M
  15. */
  16. onDown(e){
  17. wx.getSavedFileList({ // 获取文件列表
  18. success(res) {
  19. res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
  20. // 删除存储的垃圾数据
  21. wx.removeSavedFile({
  22. filePath: val.filePath
  23. });
  24. })
  25. }
  26. })
  27. wx.downloadFile({ //下载
  28. url: e.target.dataset.fileurl, // 从后端获取的url地址,赋值在标签的data属性上
  29. success: function(res){
  30. const tempFilePath = res.tempFilePath;
  31. wx.saveFile({ //保存文件到本地
  32. tempFilePath,
  33. success (res) {
  34. const savedFilePath = res.savedFilePath;
  35. wx.openDocument({ //新开页面打开文档
  36. filePath: savedFilePath,
  37. showMenu: true,
  38. flieType: 'pdf',
  39. success: function (res) {
  40. console.log('打开文档成功')
  41. },
  42. fail: function(err){
  43. console.log('打开文档失败:', err)
  44. }
  45. });
  46. },
  47. fail: function (err) {
  48. console.log('保存失败:', err)
  49. }
  50. })
  51. },
  52. fail: function (err) {
  53. console.log('下载失败:', err);
  54. }
  55. })
  56. }
  57. }
  58. })

产生的问题:

以上经验证,安卓手机上文件或者图片均可预览,唯独ios手机无法打开!!!!

提示以下内容:

在ios手机上提示: openDocument:fail filetype not supported

3. 微信小程序下载文件ios无法预览-完整方案

针对ios手机,方法要设置filePath,指定文件下载后存储的路径 (本地路径), 才可预览文件

完整最新版代码如下:

思路:区分url类型,若为['doc','docx','xls','xlsx','ppt','pptx','pdf']中的一种,则执行();若为["jpg", "jpeg", "png"]中的一种,则执行()

  1. // 下载
  2. Component({
  3. properties: {
  4. lists: {
  5. type: Array,
  6. value: []
  7. }
  8. },
  9. data: {},
  10. ready: function() { },
  11. methods: {
  12. // 判断文件类型
  13. whatFileType(url){
  14. let sr = url.lastIndexOf('.') // 最后一次出现的位置
  15. let fileType = url.substr((sr+1)) // 截取url的类型
  16. return(fileType)
  17. },
  18. // 下载文件
  19. /**
  20. * 本地文件存储的大小限制为 10M
  21. */
  22. onDown(e){
  23. let fileTypes = ['doc','docx','xls','xlsx','ppt','pptx','pdf']
  24. let imageTypes = ["jpg", "jpeg", "png"]
  25. let fileType = this.whatFileType(e.target.dataset.fileurl)
  26. let fileId = e.target.dataset.filed
  27. wx.showLoading({
  28. title: '加载中',
  29. })
  30. wx.getSavedFileList({ // 获取文件列表
  31. success(res) {
  32. res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
  33. // 删除存储的垃圾数据
  34. wx.removeSavedFile({
  35. filePath: val.filePath
  36. });
  37. })
  38. }
  39. })
  40. wx.downloadFile({
  41. url: e.target.dataset.fileurl,
  42. filePath: wx.env.USER_DATA_PATH + "/"+ fileId + "."+fileType,
  43. method: 'GET',
  44. success: function(res){
  45. if(fileTypes.includes(fileType)){
  46. wx.openDocument({
  47. filePath: res.filePath,
  48. showMenu: true,
  49. flieType: fileType,
  50. success: function (res) {
  51. wx.hideLoading()
  52. console.log('打开文档成功')
  53. },
  54. fail: function(err){
  55. wx.hideLoading()
  56. console.log('打开文档失败:', err)
  57. }
  58. });
  59. }else if(imageTypes.includes(fileType)){
  60. wx.hideLoading()
  61. wx.previewImage({
  62. showmenu: true,
  63. current: res.filePath, // 当前显示图片的http链接
  64. urls: [res.filePath] // 需要预览的图片http链接列表
  65. })
  66. }else{
  67. wx.hideLoading()
  68. wx.showModal({
  69. title: '提示',
  70. content: "文件类型不支持预览",
  71. showCancel: false
  72. })
  73. }
  74. },
  75. fail: function (err) {
  76. wx.hideLoading()
  77. wx.showToast({
  78. title: "下载超时",
  79. icon: 'none'
  80. })
  81. console.log('下载失败:', err);
  82. }
  83. })
  84. }
  85. }
  86. })

最新版代码去掉了方法。直接后即可预览,忽略了保存文件方法

欢迎点赞并收藏,可以的话关注一下吧!(一键三连~)

光收藏不点赞的同学就不地道了哦!