I have a Node.js application that picks emails from Graph Explorer and saves them to an MS SQL Server database. I have an issue with my attachments though. I am trying to save the contentBytes as they are from 'Graph Explorer' but when I do this, it is truncated. The data type is of type varbinary(max). So, I switched to an alternative to save as Binary data, but this seems to be malformed as I can't convert it back to pdf. My main aim here is to save PDFs only.
Below is the snippet:
for (const attachment of attachmentData) {
if (attachment.contentType === 'application/pdf' && attachment.contentBytes) {
// Convert attachment.contentBytes to binary buffer
const contentBuffer = Buffer.from(attachment.contentBytes, 'base64');
data type conversion
const attachmentInsertResult = await transaction.request().query`
INSERT INTO Attachments (email_id, contentBytes)
OUTPUT INSERTED.id
VALUES (${emailId}, ${contentBuffer});
`;
attachmentIds.push(attachmentInsertResult.recordset[0].id);
}
}