小程序上傳文件到微信服務器,及開發者服務器獲取上傳文件
阿新 • • 發佈:2018-11-23
emp span 回調 data pre lose 數據 blank some
微信官方參考文檔:https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html
1 1 wx.chooseImage({ 2 2 success (res) { 3 3 const tempFilePaths = res.tempFilePaths //文件的位置 4 4 wx.uploadFile({ 5 5 url: ‘https://example.weixin.qq.com/upload‘, //開發者服務器訪問接口,微信服務器通過這個接口上傳文件到開發者服務器6 6 filePath: tempFilePaths[0], 7 7 name: ‘file‘, 8 8 formData: { //上傳POST參數信息 9 9 ‘user‘: ‘test‘ 10 10 }, 11 11 success (res){ //上傳成功回調函數 12 12 const data = res.data 13 13 //do something 14 14 } 15 15 }) 16 16 } 17 17 })
註意:微信服務器端向開發者服務器發起 POST請求
開發者服務器端處理:
def upload_and_get_res(request): if request.method == ‘GET‘: return HttpResponse("服務器不接受GET請求!") else: #獲取圖像數據信息 image_file = request.FILES.get(‘file‘) # file_name = image_file.name # file_size = image_file.size f = open(‘123‘, ‘wb‘) for chunk in image_file.chunks(): f.write(chunk) f.close() //文件保存完畢,後續根據業務流程處理
小程序上傳文件到微信服務器,及開發者服務器獲取上傳文件