简体   繁体   English

nodejs 编码为 base64 字符串不起作用

[英]nodejs encoding to base64 string is not working

I am trying to send a calendar event as an attachment (.ics) file in nodejs .我正在尝试在nodejs日历事件作为附件(.ics)文件发送。

I am using ical-generator library to create an event.我正在使用ical-generator库来创建一个事件。

The code below is generating an.ics file and attaching it with the email but file always comes as empty.下面的代码生成 an.ics 文件并将其与 email 附加,但文件始终为空。

I am not sure why the event content is not converting to base64.我不确定为什么事件内容没有转换为 base64。 when I log the converted content it shows the same content (not converting to base64)当我记录转换后的内容时,它显示相同的内容(不转换为 base64)

const cal = ical({ domain: "github.com", name: "my first iCal" });
// overwrite domain
cal.domain("example.net");

cal.createEvent({
  start: moment(),
  end: moment().add(1, "hour"),
  summary: "Example Event",
  description: "It works ;)",
  location: "my room",
  url: "http://example.net/",
});

 console.log('result :', cal.toString("base64"));
    // result : 
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example.net//ical-generator//EN
NAME:my first iCal
X-WR-CALNAME:my first iCal
BEGIN:VEVENT
UID:000-tnmyae@example.net
SEQUENCE:0
DTSTAMP:20210403T212902Z
DTSTART:20210403T212902Z
DTEND:20210403T222902Z
SUMMARY:Example Event
LOCATION:my room
DESCRIPTION:It works \;)
URL;VALUE=URI:http://example.net/
END:VEVENT
END:VCALENDAR

 var message = {
      html: emailBody,
      subject: "test",
      from_email: "from email",
      from_name: "sender name",
      to: [
        {
          email: "receiver email",
        },
      ],
      tags: ["test"],
      attachments: [
        {
          type: "text/calendar",
          content: cal.toString("base64"),
          name: "fileName.ics",
        },
      ],
    };

I found this encoded to base64 content from the other thread and it works just fine.我发现这个编码为 base64 来自另一个线程的内容,它工作得很好。 "/IENvb2sgc29tZSB5YXkteW8gMg0KVFJBTlNQOk9QQVFVRQ0KU0VRVUVOQ0U6MA0KU1RBVFVTOkNPTkZJUk1FRA0KRU5EOlZFVkVOVA0KRU5EOlZDQUxFTkRBUg=="

If I try abover encoded string, my calendar event works fine.如果我尝试使用以上编码的字符串,我的日历事件可以正常工作。 the file.ics also works fine.. so my guess is problem while converting that event content to base64. file.ics 也可以正常工作。所以我猜是在将该事件内容转换为 base64 时出现问题。

The ical-generator package's calendar class doesn't provide a toString() which takes the format as the parameter, like Buffer . ical ical-generator包的日历 class 不提供将格式作为参数的toString() ,例如Buffer You'll just need to convert it to Base 64 like you'd convert any other string.您只需要将其转换为 Base 64,就像转换任何其他字符串一样。

const calStr = cal.toString()
const calB64 = Buffer.from(calStr).toString('base64')
console.log('result :', calB64);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM