1. 程式人生 > >微信小程式上傳與下載檔案

微信小程式上傳與下載檔案

需要準備的工作:

①、建立微信小程式工程,編寫以下程式碼。

②、通過IDE建立springboot+web工程,編寫接收檔案以及提供下載檔案的方式,並將上傳的檔案相關資訊記錄在mysql資料庫中。具體請檢視https://www.cnblogs.com/chenfeifen/p/10261980.html

一、配置index.wxml

 1 <!--index.wxml-->
 2 <view class="container">
 3   <view class="userinfo">
 4     <button bindtap="upload">
上傳原圖</button> 5 <button bindtap="download"> 下載圖片</button> 6 </view> 7 <view class="imginfo"> 8 <block wx:for="{{tempFilePaths}}" wx:key="{{index}}"> 9 <image src="{{item }}" bindtap="listenerButtonPreviewImage" data-index="{{index}}" style
="width: 100%;"/> 10 </block> 11 <block> <image src='{{downloadPicturePath}}' bindtap='preview_download_picture'></image> 12 </block> 13 </view> 14 </view>

二、配置index.wxss

 1  1 /**index.wxss**/
 2  2 .userinfo {
 3  3   display: flex;
 4  4   /* flex-direction: column; 
*/ 5 5 align-items: center; 6 6 } 7 7 .imginfo { 8 8 display: flex; 9 9 flex-direction: column; 10 10 align-items: center; 11 11 } 12 12 .userinfo-avatar { 13 13 width: 128rpx; 14 14 height: 128rpx; 15 15 margin: 20rpx; 16 16 border-radius: 50%; 17 17 } 18 18 19 19 .userinfo-nickname { 20 20 color: #aaa; 21 21 } 22 22 23 23 .usermotto { 24 24 margin-top: 200px; 25 25 }

三、配置index.js

  1 //index.js
  2 //獲取應用例項
  3 const app = getApp()
  4 Page({
  5   /**
  6    * 頁面的初始資料
  7    */
  8   data: {
  9     tempFilePaths: [],
 10     downloadPicturePath:''
 11   },
 12   /**
 13    * 上傳圖片方法
 14    */
 15   upload: function () {
 16     let that = this;
 17     wx.chooseImage({
 18       count: 9, // 預設9
 19       sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有
 20       sourceType: ['album', 'camera'], // 可以指定來源是相簿還是相機,預設二者都有
 21       success: res => {
 22         wx.showToast({
 23           title: '正在上傳...',
 24           icon: 'loading',
 25           mask: true,
 26           duration: 1000
 27         })  
 28         // 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片
 29         let tempFilePaths = res.tempFilePaths;
 30         that.setData({
 31           tempFilePaths: tempFilePaths
 32         })
 33         /**
 34          * 上傳完成後把檔案上傳到伺服器
 35          */
 36         var count = 0;
 37           //上傳檔案
 38         for (var i = 0; i < this.data.tempFilePaths.length;i++){
 39           wx.uploadFile({
 40             url: "http://*****/upload",//請求上傳的url
 41             filePath: tempFilePaths[i],
 42             name: 'filename',
 43             header: {
 44               "Content-Type": "multipart/form-data"
 45             },
 46             success: function (res) {
 47               count++;
 48               //如果是最後一張,則隱藏等待中  
 49               if (count == tempFilePaths.length) {
 50                 wx.hideToast();
 51               }
 52               wx.showToast({
 53                 title: '上傳成功',
 54                 icon: '',
 55                 mask: true,
 56                 duration: 1500
 57               })  
 58             },
 59             fail: function (res) {
 60               wx.hideToast();
 61               wx.showModal({
 62                 title: '錯誤提示',
 63                 content: '上傳圖片失敗',
 64                 showCancel: false,
 65                 success: function (res) { }
 66               })
 67             }
 68           });
 69         }
 70       }
 71     })
 72   },
 73   /**
 74    * 預覽下載的圖片
 75    */
 76   preview_download_picture:function(){
 77       wx.previewImage({
 78         current: this.data.downloadPicturePath,
 79         urls: this.data.downloadPicturePath,
 80       })
 81   },
 82     /**
 83    * 下載圖片方法
 84    */
 85   download:function(){
 86     var that = this;
 87     wx.downloadFile({
 88        url:"http://******/download", //僅為示例,並非真實的資源
 89       success: function (res) {
 90         console.log(res)
 91         // 只要伺服器有響應資料,就會把響應內容寫入檔案並進入 success 回撥,業務需要自行判斷是否下載到了想要的內容
 92         if (res.statusCode === 200) {
 93           wx.playVoice({
 94              filePath: res.tempFilePath
 95           })
 96           wx.showToast({
 97             title: '下載成功',
 98             icon: '',
 99             mask: true,
100             duration: 1500
101           })
102           that.setData({
103             downloadPicturePath: res.tempFilePath//將下載的圖片路徑傳給頁面顯示
104           })
105         }
106         //儲存下載的圖片到本地
107         // wx.saveImageToPhotosAlbum({
108         //     filePath: res.tempFilePath,
109         //   success:
110         //     function (data) {
111         //       console.log(data);
112         //       // wx.showModal({
113         //       //   title: '下載成功',
114         //       //   content: '下載成功',
115         //       // })
116         //       wx.showToast({
117         //         title: '下載成功',
118         //         icon: '',
119         //         mask: true,
120         //         duration: 1500
121         //       })  
122         //       that.setData({
123         //         downloadPicturePath: res.tempFilePath
124         //       })
125         //     },
126         // })
127       }
128     });
129   },
130   /**
131    * 預覽圖片方法
132    */
133   listenerButtonPreviewImage: function (e) {
134     let index = e.target.dataset.index;
135     let that = this;
136     wx.previewImage({
137       current: that.data.tempFilePaths[index],
138       urls: that.data.tempFilePaths,
139       //這根本就不走
140       success: function (res) {
141         //console.log(res);
142       },
143       //也根本不走
144       fail: function () {
145         //console.log('fail')
146       }
147     })
148   },
149 
150   /**
151    * 生命週期函式--監聽頁面載入
152    */
153   onLoad: function (options) {
154     
155   },
156 
157   /**
158    * 生命週期函式--監聽頁面初次渲染完成
159    */
160   onReady: function () {
161     
162   },
163 
164   /**
165    * 生命週期函式--監聽頁面顯示
166    */
167   onShow: function () {
168     
169   },
170 
171   /**
172    * 生命週期函式--監聽頁面隱藏
173    */
174   onHide: function () {
175     
176   },
177 
178   /**
179    * 生命週期函式--監聽頁面解除安裝
180    */
181   onUnload: function () {
182     
183   },
184 
185   /**
186    * 頁面相關事件處理函式--監聽使用者下拉動作
187    */
188   onPullDownRefresh: function () {
189     
190   },
191 
192   /**
193    * 頁面上拉觸底事件的處理函式
194    */
195   onReachBottom: function () {
196     
197   },
198 
199   /**
200    * 使用者點選右上角分享
201    */
202   onShareAppMessage: function () {
203     
204   }
205 })