Trello API将base64数据图像添加为附件

时间:2017-01-19 15:44:55

标签: api trello

我想通过API将base64图像作为附件发送到trello卡

POST /1/cards/[card id or shortlink]/attachments

有一个文件字段,但它没有指定应该如何查看数据。

参考:https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-attachments

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

简短回答:Trello的API仅用于通过multipart / form-data附加二进制数据。以下示例。

答案很长: 添加附件和图像的Trello API令人沮丧地记录不足。他们确实有一个coffeescript Client.js,我们这些人使用Javascript来简化所有基本操作:https://trello.com/1/client.coffee

使用vanilla Client.js文件,我已经能够附加链接和文本文件。虽然CURL示例显示从硬盘驱动器中提取二进制文件,但对于我们这些完全不在我们没有权限创建文件的服务器或客户端上的人来说,这并不适用。

从大量的反复试验中,我确定必须使用multipart / form-data附加所有二进制数据(图像,文档等)。这是一个jQuery片段,它将URL作为一个项目,将其放入内存,然后将其发送给Trello。

var opts = {'key': 'YOUR TRELLO KEY', 'token': 'YOUR TRELLO TOKEN'};
var xhr = new XMLHttpRequest();
  xhr.open('get', params); // params is a URL to a file to grab
  xhr.responseType = 'blob'; // Use blob to get the file's mimetype
  xhr.onload = function() {
    var fileReader = new FileReader();
    fileReader.onload = function() {
      var filename = (params.split('/').pop().split('#')[0].split('?')[0]) || params || '?'; // Removes # or ? after filename
      var file = new File([this.result], filename);
      var form = new FormData(); // this is the formdata Trello needs
      form.append("file", file);
      $.each(['key', 'token'], function(iter, item) {
        form.append(item, opts.data[item] || 'ERROR! Missing "' + item + '"');
      });
      $.extend(opts, {
        method: "POST",
        data: form,
        cache: false,
        contentType: false,
        processData: false
      });
      return $.ajax(opts);
    };
    fileReader.readAsArrayBuffer(xhr.response); // Use filereader on blob to get content
  };
  xhr.send();

我已经为Trello开发人员支持提交了一份新的coffeescript,以考虑上传以替换Client.js。它增加了一个" Trello.upload(url)"这样做有用。

为方便起见,我也附在此处。



// Generated by CoffeeScript 1.12.4
(function() {
  var opts={"version":1,"apiEndpoint":"https://api.trello.com","authEndpoint":"https://trello.com"};
  var deferred, isFunction, isReady, ready, waitUntil, wrapper,
    slice = [].slice;

  wrapper = function(window, jQuery, opts) {
    var $, Trello, apiEndpoint, authEndpoint, authorizeURL, baseURL, collection, fn, fn1, i, intentEndpoint, j, key, len, len1, localStorage, location, parseRestArgs, readStorage, ref, ref1, storagePrefix, token, type, version, writeStorage;
    $ = jQuery;
    key = opts.key, token = opts.token, apiEndpoint = opts.apiEndpoint, authEndpoint = opts.authEndpoint, intentEndpoint = opts.intentEndpoint, version = opts.version;
    baseURL = apiEndpoint + "/" + version + "/";
    location = window.location;
    Trello = {
      version: function() {
        return version;
      },
      key: function() {
        return key;
      },
      setKey: function(newKey) {
        key = newKey;
      },
      token: function() {
        return token;
      },
      setToken: function(newToken) {
        token = newToken;
      },
      rest: function() {
        var args, error, method, params, path, ref, success;
        method = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
        ref = parseRestArgs(args), path = ref[0], params = ref[1], success = ref[2], error = ref[3];
        opts = {
          url: "" + baseURL + path,
          type: method,
          data: {},
          dataType: "json",
          success: success,
          error: error
        };
        if (!$.support.cors) {
          opts.dataType = "jsonp";
          if (method !== "GET") {
            opts.type = "GET";
            $.extend(opts.data, {
              _method: method
            });
          }
        }
        if (key) {
          opts.data.key = key;
        }
        if (token) {
          opts.data.token = token;
        }
        if (params != null) {
          $.extend(opts.data, params);            
        }
        if (method === 'UPLOAD' && typeof (params) === "string" && params.length > 5) {
          var xhr = new XMLHttpRequest();
          xhr.open('get', params);
          xhr.responseType = 'blob'; // Use blob to get the mimetype
          xhr.onload = function() {
            var fileReader = new FileReader();
            fileReader.onload = function() {
              var filename = (params.split('/').pop().split('#')[0].split('?')[0]) || params || '?'; // Removes # or ? after filename
              var file = new File([this.result], filename);
              var form = new FormData();
              form.append("file", file);
              $.each(['key', 'token'], function(iter, item) {
                form.append(item, opts.data[item] || 'ERROR! Missing "' + item + '"');
              });
              $.extend(opts, {
                method: "POST",
                data: form,
                cache: false,
                contentType: false,
                processData: false
              });
              return $.ajax(opts);
            };
            fileReader.readAsArrayBuffer(xhr.response); // Use filereader on blob to get content
          };
          xhr.send();
        } else {
          return $.ajax(opts);
        }
      },
      authorized: function() {
        return token != null;
      },
      deauthorize: function() {
        token = null;
        writeStorage("token", token);
      },
      authorize: function(userOpts) {
        var k, persistToken, ref, regexToken, scope, v;
        opts = $.extend(true, {
          type: "redirect",
          persist: true,
          interactive: true,
          scope: {
            read: true,
            write: false,
            account: false
          },
          expiration: "30days"
        }, userOpts);
        regexToken = /[&#]?token=([0-9a-f]{64})/;
        persistToken = function() {
          if (opts.persist && (token != null)) {
            return writeStorage("token", token);
          }
        };
        if (opts.persist) {
          if (token == null) {
            token = readStorage("token");
          }
        }
        if (token == null) {
          token = (ref = regexToken.exec(location.hash)) != null ? ref[1] : void 0;
        }
        if (this.authorized()) {
          persistToken();
          location.hash = location.hash.replace(regexToken, "");
          return typeof opts.success === "function" ? opts.success() : void 0;
        }
        if (!opts.interactive) {
          return typeof opts.error === "function" ? opts.error() : void 0;
        }
        scope = ((function() {
          var ref1, results;
          ref1 = opts.scope;
          results = [];
          for (k in ref1) {
            v = ref1[k];
            if (v) {
              results.push(k);
            }
          }
          return results;
        })()).join(",");
        switch (opts.type) {
          case "popup":
            (function() {
              var authWindow, height, left, origin, receiveMessage, ref1, top, width;
              waitUntil("authorized", (function(_this) {
                return function(isAuthorized) {
                  if (isAuthorized) {
                    persistToken();
                    return typeof opts.success === "function" ? opts.success() : void 0;
                  } else {
                    return typeof opts.error === "function" ? opts.error() : void 0;
                  }
                };
              })(this));
              width = 420;
              height = 470;
              left = window.screenX + (window.innerWidth - width) / 2;
              top = window.screenY + (window.innerHeight - height) / 2;
              origin = (ref1 = /^[a-z]+:\/\/[^\/]*/.exec(location)) != null ? ref1[0] : void 0;
              authWindow = window.open(authorizeURL({
                return_url: origin,
                callback_method: "postMessage",
                scope: scope,
                expiration: opts.expiration,
                name: opts.name
              }), "trello", "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
              receiveMessage = function(event) {
                var ref2;
                if (event.origin !== authEndpoint || event.source !== authWindow) {
                  return;
                }
                if ((ref2 = event.source) != null) {
                  ref2.close();
                }
                if ((event.data != null) && /[0-9a-f]{64}/.test(event.data)) {
                  token = event.data;
                } else {
                  token = null;
                }
                if (typeof window.removeEventListener === "function") {
                  window.removeEventListener("message", receiveMessage, false);
                }
                isReady("authorized", Trello.authorized());
              };
              return typeof window.addEventListener === "function" ? window.addEventListener("message", receiveMessage, false) : void 0;
            })();
            break;
          default:
            window.location = authorizeURL({
              redirect_uri: location.href,
              callback_method: "fragment",
              scope: scope,
              expiration: opts.expiration,
              name: opts.name
            });
        }
      },
      addCard: function(options, next) {
        var baseArgs, getCard;
        baseArgs = {
          mode: 'popup',
          source: key || window.location.host
        };
        getCard = function(callback) {
          var height, left, returnUrl, top, width;
          returnUrl = function(e) {
            var data;
            window.removeEventListener('message', returnUrl);
            try {
              data = JSON.parse(e.data);
              if (data.success) {
                return callback(null, data.card);
              } else {
                return callback(new Error(data.error));
              }
            } catch (error1) {}
          };
          if (typeof window.addEventListener === "function") {
            window.addEventListener('message', returnUrl, false);
          }
          width = 500;
          height = 600;
          left = window.screenX + (window.outerWidth - width) / 2;
          top = window.screenY + (window.outerHeight - height) / 2;
          return window.open(intentEndpoint + "/add-card?" + $.param($.extend(baseArgs, options)), "trello", "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
        };
        if (next != null) {
          return getCard(next);
        } else if (window.Promise) {
          return new Promise(function(resolve, reject) {
            return getCard(function(err, card) {
              if (err) {
                return reject(err);
              } else {
                return resolve(card);
              }
            });
          });
        } else {
          return getCard(function() {});
        }
      }
    };
    ref = ["GET", "PUT", "POST", "DELETE", "UPLOAD"];
    fn = function(type) {
      return Trello[type.toLowerCase()] = function() {
        return this.rest.apply(this, [type].concat(slice.call(arguments)));
      };
    };
    for (i = 0, len = ref.length; i < len; i++) {
      type = ref[i];
      fn(type);
    }
    Trello.del = Trello["delete"];
    ref1 = ["actions", "cards", "checklists", "boards", "lists", "members", "organizations", "lists"];
    fn1 = function(collection) {
      return Trello[collection] = {
        get: function(id, params, success, error) {
          return Trello.get(collection + "/" + id, params, success, error);
        }
      };
    };
    for (j = 0, len1 = ref1.length; j < len1; j++) {
      collection = ref1[j];
      fn1(collection);
    }
    window.Trello = Trello;
    authorizeURL = function(args) {
      var baseArgs;
      baseArgs = {
        response_type: "token",
        key: key
      };
      return authEndpoint + "/" + version + "/authorize?" + $.param($.extend(baseArgs, args));
    };
    parseRestArgs = function(arg) {
      var error, params, path, success;
      path = arg[0], params = arg[1], success = arg[2], error = arg[3];
      if (isFunction(params)) {
        error = success;
        success = params;
        params = {};
      }
      path = path.replace(/^\/*/, "");
      return [path, params, success, error];
    };
    localStorage = window.localStorage;
    if (localStorage != null) {
      storagePrefix = "trello_";
      readStorage = function(key) {
        return localStorage[storagePrefix + key];
      };
      writeStorage = function(key, value) {
        if (value === null) {
          return delete localStorage[storagePrefix + key];
        } else {
          return localStorage[storagePrefix + key] = value;
        }
      };
    } else {
      readStorage = writeStorage = function() {};
    }
  };

  deferred = {};

  ready = {};

  waitUntil = function(name, fx) {
    if (ready[name] != null) {
      return fx(ready[name]);
    } else {
      return (deferred[name] != null ? deferred[name] : deferred[name] = []).push(fx);
    }
  };

  isReady = function(name, value) {
    var fx, fxs, i, len;
    ready[name] = value;
    if (deferred[name]) {
      fxs = deferred[name];
      delete deferred[name];
      for (i = 0, len = fxs.length; i < len; i++) {
        fx = fxs[i];
        fx(value);
      }
    }
  };

  isFunction = function(val) {
    return typeof val === "function";
  };

  wrapper(window, jQuery, opts);

}).call(this);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
&#13;
&#13;
&#13;