通过HTTP API上传的jpeg文件用HTTP API下载后无法显示,从小程序里传入的文件就可以?
发布于 5 年前 作者 sunxiulan 12849 次浏览 来自 官方Issues

下载是可以下载,但是下载的文件打开像这样:

小白,跪求大佬解答!

python代码是这么写的:

import requests
import json

APP_ID = '***'
APP_SECRET = '***'
ENV = '***'
WECHAT_URL = "https://api.weixin.qq.com/"

def get_access_token():
    url = '{0}cgi-bin/token?grant_type=client_credential&appid={1}&secret={2}'.format(WECHAT_URL, APP_ID, APP_SECRET)
    response = requests.get(url)
    result = response.json()
    print(result)
    return result['access_token']

def get_upload_url(token, env, path):
    post_url = "https://api.weixin.qq.com/tcb/uploadfile?access_token=" + token
    playload = json.dumps({"env": env, "path": path})
    upload = requests.post(post_url, data=playload)
    return upload.json()

def parse_form(res):
    form = {}
    form["key"] = res["url"].split("/")[-1]
    form["Signature"] = res["authorization"]
    form["x-cos-security-token"] = res["token"]
    form["x-cos-meta-fileid"] = res["cos_file_id"]
    return form, res["url"]

def upload(res, file):
    form = res[0]
    upload_url = res[1]
    with open(file, "rb") as f:
        form["file"] = f.read()
        success = requests.post(upload_url, files=form)

def get_download_url(token, env, file, time):
    playload = {}
    playload["env"] = env
    playload["file_list"] = []
    for item in file:
        file_dic = {}
        file_dic["fileid"] = item
        file_dic["max_age"] = time
        playload["file_list"].append(file_dic)
    post_url = "https://api.weixin.qq.com/tcb/batchdownloadfile?access_token=" + token
    print("playload:", playload)
    download = requests.post(post_url, data=json.dumps(playload))
    print("download", download)
    return download.json()

def download(res, file):
    for i in range(len(res["file_list"])):
        download_url = res["file_list"][i]["download_url"]
        f = requests.get(download_url)
        with open(file[i], "wb") as code:
            code.write(f.content)

if __name__ == '__main__':
    TIME = 7200
    PATH = r"***/a.jpeg"
    PATH_ = [r"***/2.jpeg"]
    picLst = ['cloud://***.jpeg']
    accessToken = get_access_token()
    # RES = get_upload_url(accessToken, ENV, PATH)
    # RES = parse_form(RES)
    # upload(RES, PATH)
    RES = get_download_url(accessToken, ENV, picLst, TIME) 
    download(RES, PATH_) 
回到顶部