点这里,搜索淘宝优惠券!
点这里,搜索淘宝优惠券!

小程序上传文件到微信服务器,及开发者服务器获取上传文件

微信官方参考文档: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() //文件保存完毕,后续根据业务流程处理

 

posted @ 2018-11-23 15:29  chd_ailex  阅读(3208)  评论(0编辑  收藏  举报