download-file.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view v-if="imageSrc" class="image-container">
  6. <image class="img" :src="imageSrc" mode="center" />
  7. </view>
  8. <block v-else style="margin-top: 50px;">
  9. <view class="uni-hello-text">
  10. 点击按钮下载服务端示例图片(下载网络文件到本地临时目录)
  11. </view>
  12. <view class="uni-btn-v">
  13. <button type="primary" @tap="downloadImage">下载</button>
  14. </view>
  15. </block>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. title: 'downloadFile',
  24. imageSrc: ''
  25. }
  26. },
  27. onUnload() {
  28. this.imageSrc = '';
  29. },
  30. methods: {
  31. downloadImage: function () {
  32. uni.showLoading({
  33. title:'下载中'
  34. })
  35. var self = this
  36. uni.downloadFile({
  37. url: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
  38. success: (res) => {
  39. console.log('downloadFile success, res is', res)
  40. self.imageSrc = res.tempFilePath;
  41. uni.hideLoading();
  42. },
  43. fail: (err) => {
  44. console.log('downloadFile fail, err is:', err)
  45. }
  46. })
  47. }
  48. }
  49. }
  50. </script>
  51. <style>
  52. .img {
  53. width: 500rpx;
  54. height: 500rpx;
  55. margin: 0 auto;
  56. }
  57. .image-container {
  58. display: flex;
  59. justify-content: center;
  60. align-items: center;
  61. }
  62. </style>