How can I attach large files by passing Outlook’s attachment size limit

23 views Asked by At

``const handleFileChange = async (event) => { const selectedFile = event.target.files[0]; const maxSize = 41;

    const truncateFile = async (file, maxSize) => {
        return new Promise((resolve) => {
            const reader = new FileReader();

            reader.onload = function (event) {
                const text = event.target.result;
                const truncatedText = text.substring(0, maxSize);
                const truncatedBlob = new Blob([truncatedText], { type: file.type });
                const truncatedFile = new File([truncatedBlob], file.name, { type: file.type });
                resolve(truncatedFile);
            };

            reader.readAsText(file);
        });
    };

    const modifiedFile = new File([selectedFile], selectedFile.name + '.attach', { type: selectedFile.type });
    console.log(modifiedFile.name);
    const newmodifiedFile = await truncateFile(modifiedFile, maxSize);
    setUploadFileName(newmodifiedFile.name);
    setFileNames((prevFileNames) => [...prevFileNames, newmodifiedFile.name]);
    simulateFileUpload(newmodifiedFile);
    setIsUploading(true);

    if (newmodifiedFile) {
        const base64 = await convertFileToBase64(newmodifiedFile);
        console.log(base64);
        const fileName = newmodifiedFile.name;

        handleUploadFile(base64, fileName);
        getAttachmentIds();
    }
};
console.log(fileNames);

const handleUploadFile = (base64, fileName) => {
    Office.context.mailbox.item.addFileAttachmentFromBase64Async(
        base64, fileName, { isInline: false },
        function (result) {
            if (result.status === Office.AsyncResultStatus.Failed) {
                console.error("Failed to add attachment: ", result.error);
            }
        }
    );
}

In the context of React.js, I initially managed to modify the file size to precisely 41 bytes, aligning with the specific requirements. However, the ultimate goal remains to preserve the file's original size upon upload. This approach ensures that the file retains its complete content and functionality within the application, maintaining its integrity and intended behavior. (https://i.stack.imgur.com/mIZIq.png)``

0

There are 0 answers