如何使用 json 响应保存文件

问题描述 投票:0回答:1

response 得到如下的 json:

{'additionalErrors': None,
 'data': {'contentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          'file': 'UEsDBBQACAAIAAAAAAAAAAAAAAAAAAAAAAATAAAAeGwvdGhlbWUvdGhlbWUxLnhtbOyZz2/bNhT...
          (there are still about 11k characters)
          ...hsL3N0eWxlcy54bWxQSwUGAAAAAAsACwDGAgAASB4AAAAA',
          'name': 'export-data.xlsx'},
 'error': False,
 'errorText': ''}

有了这些数据,是否可以将文件下载到计算机上?

我不知道如何解决这个问题

python-3.x openxml
1个回答
0
投票

当您收到此响应时,您已经下载了该文件,它只是以 Base64 格式编码。要解码它,你可以这样做:

import base64

resp = ... # get your JSON response

decoded = base64.b64decode(resp["data"]["file"])
with open("/some/path", "wb") as f:
    f.write(decoded)

© www.soinside.com 2019 - 2024. All rights reserved.