Hi Tasha,
Thanks a lot for your reply, helped me to solve the above issue by using api.intercept
I am not able to use restDataProvider since I am using Typescript and I have client generated files which provides a wrapper to call my backend api
But I had to convert the file object suitable for my backend
const reconstructFile = (fw: any): File => {
console.log("Reconstructing file:", fw);
return new File([fw.file.file], fw.file.name, { type: fw.file?.type || "application/octet-stream" });
};
function arrayBufferToFile(buffer: ArrayBuffer, filename: string, mimeType: string): File {
const blob = new Blob([buffer], { type: mimeType });
return new File([blob], filename, { type: mimeType });
}
const init = (api: any) => {
api.intercept("create-file", (fileWrapper: any) => {
const file = reconstructFile(fileWrapper);
console.log("File object:", file);
if (!(file instanceof File)) {
console.error("Invalid File object:", file);
return;
}
const reader = new FileReader();
reader.onload = async () => {
const arrayBuffer = reader.result as ArrayBuffer;
const attachmentId = crypto.randomUUID();
const uploadData = {
id: attachmentId,
name: file.name,
file: arrayBufferToFile(arrayBuffer, file.name, file.type)
};
try {
const response = await commandApi.file.uploadFile(uploadData);
if (response?.data?.data) {
console.log("File uploaded successfully", response.data.data);
}
} catch (err) {
console.error("File upload failed", err);
}
};
reader.readAsArrayBuffer(file);
});
is there any better way to do this ?