当前位置:   article > 正文

uniapp保存图片到本地手机相册_如何使用uni.downloadfile下载本地图片

如何使用uni.downloadfile下载本地图片

父组件

<downReport :dataImage="dataImage"></downReport>

子组件

  1. <view class="down-report">
  2. <view class="down-btn" @click="downReport">下载报告</view>
  3. </view>
  1. <script>
  2. export default {
  3. props: ["dataImage"],
  4. methods: {
  5. // 用户下载图片并授权相册权限
  6. downLoadFile() {
  7. uni.downloadFile({
  8. url: this.dataImage, //图片地址
  9. success: function(res) {
  10. console.log('resvvv', res);
  11. //wx.saveImageToPhotosAlbum方法:保存图片到系统相册
  12. uni.saveImageToPhotosAlbum({
  13. filePath: res.tempFilePath, //图片文件路径
  14. success: function(data) {
  15. uni.hideLoading(); //隐藏 loading 提示框
  16. uni.showModal({
  17. title: '提示',
  18. content: '保存成功',
  19. modalType: false,
  20. })
  21. },
  22. // 接口调用失败的回调函数
  23. fail: function(err) {
  24. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err
  25. .errMsg === "saveImageToPhotosAlbum:fail auth deny" || err
  26. .errMsg === "saveImageToPhotosAlbum:fail authorize no response"
  27. ) {
  28. wx.showModal({
  29. title: '提示',
  30. content: '需要您授权保存相册',
  31. modalType: false,
  32. success: modalSuccess => {
  33. wx.openSetting({
  34. success(settingdata) {
  35. console.log("settingdata",
  36. settingdata)
  37. if (settingdata
  38. .authSetting[
  39. 'scope.writePhotosAlbum'
  40. ]) {
  41. wx.showModal({
  42. title: '提示',
  43. content: '获取权限成功,再次点击图片即可保存',
  44. modalType: false,
  45. })
  46. } else {
  47. wx.showModal({
  48. title: '提示',
  49. content: '获取权限失败,将无法保存到相册哦~',
  50. modalType: false,
  51. })
  52. }
  53. },
  54. fail(failData) {
  55. console.log("failData",
  56. failData)
  57. },
  58. complete(finishData) {
  59. console.log("finishData",
  60. finishData)
  61. }
  62. })
  63. }
  64. })
  65. }
  66. },
  67. complete(res) {
  68. wx.hideLoading(); //隐藏 loading 提示框
  69. }
  70. })
  71. }
  72. })
  73. },
  74. downReport() {
  75. console.log('下载报告');
  76. uni.showLoading({
  77. title: '加载中...'
  78. });
  79. this.downLoadFile()
  80. }
  81. }
  82. }
  83. </script>
  1. <style lang="scss">
  2. .down-report {
  3. height: 182rpx;
  4. margin-top: 29rpx;
  5. // border: 1px solid green;
  6. position: relative;
  7. background-color: #fff;
  8. .down-btn {
  9. width: 690rpx;
  10. height: 90rpx;
  11. line-height: 90rpx;
  12. background: #C41335;
  13. border-radius: 45rpx;
  14. position: absolute;
  15. left: 50%;
  16. top: 20rpx;
  17. transform: translateX(-50%);
  18. text-align: center;
  19. font-size: 30rpx;
  20. color: #fff;
  21. }
  22. }
  23. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/572247
推荐阅读
相关标签
  

闽ICP备14008679号