在trello api文档中,我们具有此POST方法,可使用curl将文件上传到卡中。
curl -v -F token = {TrelloToken} -F file=@test.pdf https://api.trello.com/1/cards/5bbce18fa4a337483b145a57/attachments \?key \ = {APIKey} \&token \ = {TrelloToken}
当我使用C#时,我确实构建了此方法来尝试上传图像,但事实证明文件上传(或不上传),但似乎文件范围不正确。我实际上不知道该文件是否是我请求上传的文件,因为下载文件时无法打开。随附的图像显示了我进行的测试的结果。 link中包含文档,但我不知道自己在哪里。
public static string CreateCard(string title,string description,string listId,string imagePath,string key,string token)
{
string id = string.Empty;
string url = "https://api.trello.com/1/cards";
Cards card = new Cards();
card.name = title;
card.desc = description;
card.idList = listId;
card.pos = "bottom";
card.key = key;
card.token = token;
card.keepFromSource = "all";
string jsonData = JsonConvert.SerializeObject(card);
string serverResponse = GenericPost(url,jsonData,token,key);
var objId = JObject.Parse(serverResponse)["id"];
id = objId.ToString();
UploadAttachment(key,id,imagePath,title);
return id;
}
public static void UploadAttachment(string key,string token,string cardId,string name)
{
System.Windows.Forms.MessageBox.Show(imagePath);
string url = $"https://api.trello.com/1/cards/{cardId}/attachments";
Attachments at = new Attachments();
at.token = token;
at.key = key;
at.id = cardId;
at.file = imagePath;
at.mimeType = "image/jpeg";
at.name = name;
string json = JsonConvert.SerializeObject(at);
string respo= GenericPostImage(url,json,key);
}
public static string GenericPostImage(string url,string JSONpostData,string key)
{
var request = (HttpWebRequest)WebRequest.Create(url);
string header = $"key={key}&token={token}";
request.Headers.Add(HttpRequestHeader.Authorization,header);
request.Method = "POST";
request.ContentType = "application/json";
using (StreamWriter wtr = new StreamWriter(request.GetRequestStream()))
{
wtr.Write(JSONpostData);
wtr.Close();
}
Httpwebresponse response = null;
string strResponseValue = string.Empty;
try
{
response = (Httpwebresponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
strResponseValue = reader.ReadToEnd();
}
}
}
}
catch
{
System.Windows.Forms.MessageBox.Show("Error");
}
return strResponseValue;
}