Serhii Stepanenko
05/06/2024, 2:39 PMconst fileData = uploadFile.buffer.data;
const fileType = mimeTypeToFileType[uploadFile.mimetype];
log.debug(LOGTITLE, `fileType: ${uploadFile.mimetype}, ${fileType}`);
let binary = '';
const bytes = new Uint8Array(fileData);
bytes.forEach(byte => binary += String.fromCharCode(byte)); // For txt files, the binary value is identical to the original file content.
let base64 = '';
for (let i = 0; i < binary.length; i += 3) {
const byte1 = binary.charCodeAt(i);
const byte2 = binary.charCodeAt(i + 1);
const byte3 = binary.charCodeAt(i + 2);
const chr1 = byte1 >> 2;
const chr2 = ((byte1 & 3) << 4) | (byte2 >> 4);
let chr3 = ((byte2 & 15) << 2) | (byte3 >> 6);
let chr4 = byte3 & 63;
if (isNaN(byte2)) {
chr3 = chr4 = 64;
} else if (isNaN(byte3)) {
chr4 = 64;
}
base64 += String.fromCharCode(chr1 + 65, chr2 + 65, chr3 + 65, chr4 + 65);
}
const fileObj = file.create({
name: uploadFile.originalname,
fileType: fileType,
contents: base64, // Need to convert this value to binary to make your txt file work
folder: requestBody.folderId,
});
const fileId = fileObj.save();
battk
05/06/2024, 4:45 PMSerhii Stepanenko
05/06/2024, 4:46 PMbattk
05/06/2024, 4:51 PMSerhii Stepanenko
05/06/2024, 4:51 PMbattk
05/06/2024, 4:52 PMSerhii Stepanenko
05/06/2024, 4:53 PMbattk
05/06/2024, 4:53 PMbattk
05/06/2024, 4:55 PMbattk
05/06/2024, 4:55 PMSerhii Stepanenko
05/06/2024, 4:56 PMbattk
05/06/2024, 4:56 PMbattk
05/06/2024, 4:57 PMbattk
05/06/2024, 4:58 PMSerhii Stepanenko
05/06/2024, 4:59 PMbattk
05/06/2024, 4:59 PMbattk
05/06/2024, 5:00 PMSerhii Stepanenko
05/06/2024, 5:00 PMbattk
05/06/2024, 5:02 PMbattk
05/06/2024, 5:02 PMSerhii Stepanenko
05/06/2024, 5:03 PM