【问题标题】:Trello attachment serving wrong Content-Type when attachment uploaded via the API通过 API 上传附件时,Trello 附件提供错误的 Content-Type
【发布时间】:2017-02-15 18:18:02
【问题描述】:

我正在使用 Trello API 将文件上传(和附加)到卡片。

我给https://api.trello.com/1/cards/my-card-id/attachments发了一个帖子

消息正文是 JSON

{ file: file_contents, 'BuildSheet.html': filename, mimeType: 'text/html' }

file_contents is a string that contains the body of the file I want to attach.

这行得通。该文件被上传并附加。当我获取卡片数据时,这就是我看到的有关此附件的内容。

{"id":"58a496bc751c0c2fa260630f",
 "bytes":3291,
 "date":"2017-0215T17:58:20.881Z",
 "edgeColor":null, 
 "idMember":"55240806b8ca85db897253c4",
 "isUpload":true,
 "mimeType":"text/html",
 "name":"BuildSheet.html",
 "previews":[],
 "url":"https://trello-attachments.s3.amazonaws.com/589ca323806c1d80cc03ea12/589ceda619d5936e8428f15b/1f62074b6700e61e611a90beaa8c2c73/Upload"}

您可以看到 mimeType 设置正确。名字也是正确的。但是,如果您从 UI 内部上传,则 url 不会使用文件名。所以该文件没有 .html 扩展名。

当我下载文件时,它包含这个标题

Content-Type: application/octet-stream

应该是文本/html。这会导致浏览器下载文件而不是显示它。

我做错了吗?有没有其他人遇到过这个问题?

另外有没有办法让 Trello 在构造 url 时使用文件名?

【问题讨论】:

    标签: trello


    【解决方案1】:

    Trello API 似乎忽略了mimeType 参数,而是在响应正文中使用了Content-Disposition 参数。

    一个像下面这样的身体工作:

    ------WebKitFormBoundarydjg0lJJiuTZmCppw
    Content-Disposition: form-data; name="file"; filename="image.png"
    Content-Type: image/png
    
    

    下面是对应的代码,使用节点请求库:

    async function attachFileToTrelloCard(id: string, file: Buffer, fileName: string, mimeType: string) {
      const options = {
        method: 'POST',
        url: `https://api.trello.com/1/cards/${id}/attachments`,
        formData: {
          file: {
            value: file,
            options: {
              filename: fileName,
              contentType: mimeType
            }
          },
          token: process.env.TRELLO_API_TOKEN,
          key: process.env.TRELLO_API_KEY,
          name: fileName,
          mimeType
        }
      }
    
      return await request(options)
    }
    

    【讨论】:

    • 确保您的文件名中包含扩展名。我已将文件名设置为“屏幕截图”,然后预览不起作用。当我改为将其重命名为“Screenshot.jpg”时,预览工作。感觉 Trello 以某种方式读取了扩展的 mimeType,可能没有考虑到您提供的 mimeType?
    【解决方案2】:

    当我将文件或 URL 附加到 Trello 卡时,我只使用带有 url 的 POST,如下所示 (JavaScript):

    var id = 'something';
    var attach = 'https://www.cs.tut.fi/~jkorpela/forms/file.html';
    
    var payload = {"url": attach};
    var blob = new Blob([JSON.stringify(payload)], {type: 'application/json'});
    var url = 'https://api.trello.com/1/cards/'+id+'/attachments?key='+API_KEY+'&token='+TOKEN;
    
    var xhttp = new XMLHttpRequest();   
    xhttp.open("POST", url, true);
    xhttp.send(blob);
    

    之后,当我得到卡片的 JSON 时,它是这样的:

    {"id":"xxx...", ... 
     "actions":[{
       "id":"yyy...",
       "idMemberCreator":"...",
       "data":{
         "board":{ ...
         "attachment":{
           "url":"https://www.cs.tut.fi/~jkorpela/forms/file.html",
           "name":"https://www.cs.tut.fi/~jkorpela/forms/file.html",
           "id":"zzz..."}}, ...
    

    您可以通过在网址末尾添加.json 来获取卡片的 JSON。您可以看到nameurl 相同,但这里没有mimeType。在the documentation 中找不到它。您以哪种方式“获取卡数据”?如果您使用 JavaScript,上面的代码可能会对您有所帮助。

    而关于卡片url的自定义,我什么都不知道,估计是不可能的。

    【讨论】:

      【解决方案3】:

      { 文件:file_contents,'BuildSheet.html':文件名,mimeType:'text/html'}

      您是否尝试过使用密钥namereference

      【讨论】:

        猜你喜欢
        • 2019-05-20
        • 2016-09-03
        • 2011-03-04
        • 2021-09-18
        • 1970-01-01
        • 1970-01-01
        • 2018-05-14
        • 1970-01-01
        • 2012-11-04
        相关资源
        最近更新 更多