<small id='YDo6C'></small><noframes id='YDo6C'>

    <bdo id='YDo6C'></bdo><ul id='YDo6C'></ul>
<legend id='YDo6C'><style id='YDo6C'><dir id='YDo6C'><q id='YDo6C'></q></dir></style></legend>
  • <i id='YDo6C'><tr id='YDo6C'><dt id='YDo6C'><q id='YDo6C'><span id='YDo6C'><b id='YDo6C'><form id='YDo6C'><ins id='YDo6C'></ins><ul id='YDo6C'></ul><sub id='YDo6C'></sub></form><legend id='YDo6C'></legend><bdo id='YDo6C'><pre id='YDo6C'><center id='YDo6C'></center></pre></bdo></b><th id='YDo6C'></th></span></q></dt></tr></i><div id='YDo6C'><tfoot id='YDo6C'></tfoot><dl id='YDo6C'><fieldset id='YDo6C'></fieldset></dl></div>

        <tfoot id='YDo6C'></tfoot>

        node.js Gmail API:获取内联/嵌入式图像

        node.js Gmail API: Getting Inline/Embedded images(node.js Gmail API:获取内联/嵌入式图像)
        <legend id='Pwi25'><style id='Pwi25'><dir id='Pwi25'><q id='Pwi25'></q></dir></style></legend>

        1. <i id='Pwi25'><tr id='Pwi25'><dt id='Pwi25'><q id='Pwi25'><span id='Pwi25'><b id='Pwi25'><form id='Pwi25'><ins id='Pwi25'></ins><ul id='Pwi25'></ul><sub id='Pwi25'></sub></form><legend id='Pwi25'></legend><bdo id='Pwi25'><pre id='Pwi25'><center id='Pwi25'></center></pre></bdo></b><th id='Pwi25'></th></span></q></dt></tr></i><div id='Pwi25'><tfoot id='Pwi25'></tfoot><dl id='Pwi25'><fieldset id='Pwi25'></fieldset></dl></div>
            • <bdo id='Pwi25'></bdo><ul id='Pwi25'></ul>

              <tfoot id='Pwi25'></tfoot>

                <tbody id='Pwi25'></tbody>

            • <small id='Pwi25'></small><noframes id='Pwi25'>

                  本文介绍了node.js Gmail API:获取内联/嵌入式图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在获取电子邮件时,我运行 gmail.users.messages.get(),然后运行以下两个函数来处理 payload.

                  When grabbing an email I run the gmail.users.messages.get() and then run the following two functions to process the payload.

                  function getBody(message) {
                    var encodedBody = '';  
                    try{    
                      if(typeof message.parts === 'undefined'){
                        encodedBody = message.body.data;
                      }    
                      else{
                        encodedBody = getHTMLPart(message.parts);
                      }
                      encodedBody = encodedBody.replace(/-/g, '+').replace(/_/g, '/').replace(/s/g, '');
                    }
                    catch(e) {} // there was a failure
                  
                    return decodeURIComponent(escape(window.atob(encodedBody)));
                  }
                  function getHTMLPart(arr) {
                  
                    for(var x = 0; x <= arr.length; x++){    
                      if(typeof arr[x].parts === 'undefined'){
                        if(arr[x].mimeType === 'text/html'){
                          return arr[x].body.data;
                        }
                      }
                      else{      
                        return getHTMLPart(arr[x].parts);
                      }
                    }
                    return '';
                  }
                  

                  然后我将这些数据保存到 .html 文件中以备后用.问题是内联图像没有嵌入 base64 或任何其他检索数据的方式,而是使用唯一的 CID 嵌入.

                  I then save that data into an .html file for later use. The problem is that inline images aren't embedded with base64 or any other way to retrieve that data, but are instead embedded using a unique CID.

                  所以我需要做的是,在从上述函数中检索有效负载时,我还需要检索嵌入的图像并将其在本地保存为 <CID.png>(或 jpg管他呢).然后我可以对消息进行替换,以将嵌入在 html 中的 CID 替换为图像的本地路径.

                  So what I need to do is, when retrieving the payload from the above function, I need to also retrieve the embedded image and save it locally as <CID.png> (or jpg or whatever). I can then to a replace on the message to replace the CID embed in the html with the local path of the image.

                  那么有没有人知道如何获取这些嵌入图像或有任何建议?提前致谢!

                  So does anyone know how or have any advice on how to get those embedded images? Thanks in advance!

                  推荐答案

                  图片将被提取到附件中.在响应中查找 Content-IDX-Attachment-Id 标头中包含 cid 的部分,获取附件,然后插入 base64 数据作为图像源,而不是 cid.

                  The images will be extracted into attachments. Look for the part in the response that includes the cid in the Content-ID or X-Attachment-Id headers, get the attachment, and insert the base64 data as the image source instead of the cid.

                  示例

                  var response = {
                   "id": "15ade50437b9aa01",
                   "threadId": "15ade50437b9aa01",
                   "labelIds": [
                    "UNREAD",
                    "IMPORTANT",
                    "SENT",
                    "INBOX"
                   ],
                   "snippet": "",
                   "historyId": "1171380",
                   "internalDate": "1489788486000",
                   "payload": {
                    "mimeType": "multipart/related",
                    "filename": "",
                    "headers": [
                     {
                      "name": "Content-Type",
                      "value": "multipart/related; boundary=94eb2c034184892a95054af46913"
                     }
                    ],
                    "body": {
                     "size": 0
                    },
                    "parts": [
                     {
                      "mimeType": "multipart/alternative",
                      "filename": "",
                      "headers": [
                       {
                        "name": "Content-Type",
                        "value": "multipart/alternative; boundary=94eb2c034184892a93054af46912"
                       }
                      ],
                      "body": {
                       "size": 0
                      },
                      "parts": [
                       {
                        "partId": "0.0",
                        "mimeType": "text/plain",
                        "filename": "",
                        "headers": [
                         {
                          "name": "Content-Type",
                          "value": "text/plain; charset=UTF-8"
                         }
                        ],
                        "body": {
                         "size": 25,
                         "data": "W2ltYWdlOiBJbmZvZ2FkIGJpbGQgMV0NCg=="
                        }
                       },
                       {
                        "partId": "0.1",
                        "mimeType": "text/html",
                        "filename": "",
                        "headers": [
                         {
                          "name": "Content-Type",
                          "value": "text/html; charset=UTF-8"
                         }
                        ],
                        "body": {
                         "size": 106,
                         "data": "PGRpdiBkaXI9Imx0ciI-PGltZyBzcmM9ImNpZDppaV8xNWFkZTUwMmVlYTg0MGNlIiBhbHQ9IkluZm9nYWQgYmlsZCAxIiB3aWR0aD0iNTgiIGhlaWdodD0iNTQiPjxicj48L2Rpdj4NCg=="
                        }
                       }
                      ]
                     },
                     {
                      "partId": "1",
                      "mimeType": "image/png",
                      "filename": "smile.png",
                      "headers": [
                       {
                        "name": "Content-Type",
                        "value": "image/png; name="smile.png""
                       },
                       {
                        "name": "Content-Disposition",
                        "value": "inline; filename="smile.png""
                       },
                       {
                        "name": "Content-Transfer-Encoding",
                        "value": "base64"
                       },
                       {
                        "name": "Content-ID",
                        "value": "u003cii_15ade502eea840ceu003e"
                       },
                       {
                        "name": "X-Attachment-Id",
                        "value": "ii_15ade502eea840ce"
                       }
                      ],
                      "body": {
                       "attachmentId": "ANGjdJ8Xh1_0DBjFbc2qKRHD8uTw-9nkPP30v-vohJforDg54EHPHf3Obd2P9W6Wfss0cwfmblQWi5F3958vcEi0HyiMNgpKJbsQAVP9viUOY4LzyxwAvR7-dis4PNGflBpkZFMHv62LGKkQ1-ZPG3Go_Xh_sXJUveHl4JjmwLpNp6LjlHzuA_3XOkY2LLQLFmXNTo_dJbqDQWvMb8UTGnATMOoTNKvNQ4Ndr9pgQYI1SBvtdThgUDmlOGKYLHM6qR4AlrNNFnPUCZZU-BB7o7Dt2dhj-kexiIdvaB2LEnoeCBth_oK9HELt2tw4rlY",
                       "size": 8539
                      }
                     }
                    ]
                   },
                   "sizeEstimate": 12800
                  };
                  
                  function getHtml(res) {
                    var parts = [res.payload];
                    while (parts.length) {
                      var part = parts.shift();
                      if (part.parts) {
                        parts = parts.concat(part.parts);
                      }
                  
                      if(part.mimeType === 'text/html') {
                        return decodeURIComponent(escape(atob(part.body.data.replace(/-/g, '+').replace(/\_/g, '/'))));
                      }
                    }
                    return '';
                  }
                  
                  function getAttachmentId(res, cid) {
                    var parts = [res.payload];
                    while (parts.length) {
                      var part = parts.shift();
                      if (part.parts) {
                        parts = parts.concat(part.parts);
                      }
                      var headers = part.headers;
                      var indexedHeaders = headers.reduce(function(acc, header) {
                        acc[header.name.toLowerCase()] = header.value;
                        return acc;
                      }, {});
                      var contentId = indexedHeaders['content-id'] || '';
                      var xAttachmentId = indexedHeaders['x-attachment-id'] || '';
                      if (contentId.includes(cid) || xAttachmentId.includes(cid)) {
                        return part.body.attachmentId;
                      }
                    }
                    return '';
                  }
                  
                  var html = getHtml(response);
                  console.log(html);
                  // Extract the cids and find the matching attachments in the response
                  var attachmentId = getAttachmentId(response, 'ii_15ade502eea840ce');
                  console.log(attachmentId);
                  // Get the attachment from the Gmail API and replace the cid 
                  // with the base64-data

                  这篇关于node.js Gmail API:获取内联/嵌入式图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Gradient Button to background-color blinks on hover(悬停时背景颜色的渐变按钮闪烁)
                  How to make hovering over active button not use hover effect?(如何使悬停在活动按钮上不使用悬停效果?)
                  html javascript show image hover(html javascript 显示图像悬停)
                  How to get css hover values on click?(如何在点击时获得 css 悬停值?)
                  Change background image on link hover(更改链接悬停时的背景图像)
                  Highlight multiple items on hover#39;s condition(突出显示悬停条件下的多个项目)

                  <tfoot id='cLQjJ'></tfoot>
                      <bdo id='cLQjJ'></bdo><ul id='cLQjJ'></ul>

                      <small id='cLQjJ'></small><noframes id='cLQjJ'>

                    • <legend id='cLQjJ'><style id='cLQjJ'><dir id='cLQjJ'><q id='cLQjJ'></q></dir></style></legend>

                            <tbody id='cLQjJ'></tbody>
                          <i id='cLQjJ'><tr id='cLQjJ'><dt id='cLQjJ'><q id='cLQjJ'><span id='cLQjJ'><b id='cLQjJ'><form id='cLQjJ'><ins id='cLQjJ'></ins><ul id='cLQjJ'></ul><sub id='cLQjJ'></sub></form><legend id='cLQjJ'></legend><bdo id='cLQjJ'><pre id='cLQjJ'><center id='cLQjJ'></center></pre></bdo></b><th id='cLQjJ'></th></span></q></dt></tr></i><div id='cLQjJ'><tfoot id='cLQjJ'></tfoot><dl id='cLQjJ'><fieldset id='cLQjJ'></fieldset></dl></div>