最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to attach image(s) to a new Trello card from the JS client library? - Stack Overflow

programmeradmin1浏览0评论

While adding a new card with the trello_client.js library, I want to attach an image or more. The documentation only mentions that fileSource is "A file".

Data URI didn't work as is, and blobs are usually appended to FormData objects so I'm not sure how to approach this and didn't find working examples.

And is it even possible to attach multiple files along with the card creation, or only later, by separate posts here?

While adding a new card with the trello_client.js library, I want to attach an image or more. The documentation only mentions that fileSource is "A file".

Data URI didn't work as is, and blobs are usually appended to FormData objects so I'm not sure how to approach this and didn't find working examples.

And is it even possible to attach multiple files along with the card creation, or only later, by separate posts here?

Share Improve this question asked Mar 6, 2016 at 8:50 RonnyRonny 4,5372 gold badges25 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

The client.js library doesn't support attaching file attachments. You'll need to use a standard XHR and FormData object.

Here's some sample code: https://plnkr.co/edit/PjJsfMgJuJaM5A83RAiW

The relevant bits of HTML:

<input type="file" id="chooser"/>
<button onclick="upload();">Upload</button>

and the javascript:

// Setup
var TOKEN = "";
var KEY = "";
var CARD = "";

function upload() {
  var formData = new FormData();

  formData.append("token", TOKEN);
  formData.append("key", KEY);

  // HTML file input, chosen by user
  formData.append("file", document.getElementById('chooser').files[0]);

  var request = new XMLHttpRequest();
  request.open("POST", "https://api.trello./1/cards/" + CARD + "/attachments");
  request.send(formData);
}

The Trello API expects either a URL to be attached, or a file encoded as multi-part/formdata.

I've put together a series of examples on different ways you can attach files to a card using Javascript. You can check it out here: https://glitch./~trello-attachments-api. To see the example project running live, click "Preview App."

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>