首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串作为图片上传到trello api

将字符串作为图片上传到trello api
EN

Stack Overflow用户
提问于 2021-11-05 15:11:35
回答 1查看 31关注 0票数 0

我正在试着上传一个图像文件到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

如何在上传前将此图像转换为图像?

代码语言:javascript
复制
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})
EN

回答 1

Stack Overflow用户

发布于 2021-11-05 20:15:10

使用这篇来自https://community.atlassian.com/t5/Trello-questions/Mime-types-of-attachments-uploaded-to-Trello/qaq-p/1369920的帖子

我最终得到了这段代码,它也处理mimetype corect:

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69855425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档