我正在开发一个能够读取包含图像网址列表的CSV的
javascript客户端.
我可以通过jquery-csv读取csv并在html5画布中绘制每个图像.
下一步是将每个图像应用于文本图层,并使用gmail api通过电子邮件发送图像.
所以我的不同之处在于找到一个示例,向我展示如何使用画布并仅使用javascript将其附加到电子邮件中.
我有没有根据multipart gmail指南构建一个json并将其作为指定的POST主体发送?
你能给我一些例子吗?
解决方法
// Get the canvas from the DOM and turn it into base64-encoded png data. var canvas = document.getElementById("canvas"); var dataUrl = canvas.toDataURL(); // The relevant data is after 'base64,'. var pngData = dataUrl.split('base64,')[1]; // Put the data in a regular multipart message with some text. var mail = [ 'Content-Type: multipart/mixed; boundary="foo_bar_baz"\r\n','MIME-Version: 1.0\r\n','From: sender@gmail.com\r\n','To: receiver@gmail.com\r\n','Subject: Subject Text\r\n\r\n','--foo_bar_baz\r\n','Content-Type: text/plain; charset="UTF-8"\r\n','Content-Transfer-Encoding: 7bit\r\n\r\n','The actual message text goes here\r\n\r\n','Content-Type: image/png\r\n','Content-Transfer-Encoding: base64\r\n','Content-Disposition: attachment; filename="example.png"\r\n\r\n',pngData,'\r\n\r\n','--foo_bar_baz--' ].join(''); // Send the mail! $.ajax({ type: "POST",url: "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart",contentType: "message/rfc822",beforeSend: function(xhr,settings) { xhr.setRequestHeader('Authorization','Bearer {ACCESS_TOKEN}'); },data: mail });