[英]nodejs encoding to base64 string is not working
我正在嘗試在nodejs
日歷事件作為附件(.ics)
文件發送。
我正在使用ical-generator
庫來創建一個事件。
下面的代碼生成 an.ics 文件並將其與 email 附加,但文件始終為空。
我不確定為什么事件內容沒有轉換為 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",
},
],
};
我發現這個編碼為 base64 來自另一個線程的內容,它工作得很好。 "/IENvb2sgc29tZSB5YXkteW8gMg0KVFJBTlNQOk9QQVFVRQ0KU0VRVUVOQ0U6MA0KU1RBVFVTOkNPTkZJUk1FRA0KRU5EOlZFVkVOVA0KRU5EOlZDQUxFTkRBUg=="
如果我嘗試使用以上編碼的字符串,我的日歷事件可以正常工作。 file.ics 也可以正常工作。所以我猜是在將該事件內容轉換為 base64 時出現問題。
ical ical-generator
包的日歷 class 不提供將格式作為參數的toString()
,例如Buffer
。 您只需要將其轉換為 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.