我正在试着上传一个图像文件到trello api。下面的代码完成了这项工作,但文件是以文本字符串的形式上传的。
C:/file保存一个文本字符串。如果我把这个字符串放到像https://codebeautify.org/base64-to-image-converter这样的在线base64转换器中运行,我会得到一个很好的png图像。
API文档指定我可以将filename、mimetype设置为字符串,并将文件附加为multipart/form-data Format: binary
https://developer.atlassian.com/cloud/trello/rest/api-group-cards/#api-cards-id-attachments-post
如何在上传前将此图像转换为图像?
import requests
import base64
key = "xxx"
token = "yyy"
url = "https://api.trello.com/1/cards/618390bbdc3cb10550c9912b/attachments"
pathToFile ="C:/file"
f = open(pathToFile, "r")
file = f.read()
f.close()
message_bytes = file.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')
response = requests.post(url, params={'token':token,'key':key, 'mimeType ':'png', 'name':'fisk.png'}, files={'file':base64_message})发布于 2021-11-05 20:15:10
我最终得到了这段代码,它也处理mimetype corect:
import requests
import base64
def attachFileToCardID(key,token, trelloCardID, theFile, theName, theMIMEtype):
imgdata = base64.b64decode(file)
resp = requests.post("https://trello.com/1/cards/{}/attachments".format(trelloCardID),
params={'key': key, 'token': token},
files={'file': (theName, imgdata, theMIMEtype)})
return resp.status_code
key = "xxx"
token = "yyy"
pathToFile ="C:/file.txt"
f = open(pathToFile, "r")
file = f.read()
f.close()
x = attachFileToCardID(key,token,'zzz', file, 'filename.png','image/png')https://stackoverflow.com/questions/69855425
复制相似问题