PEgaz
PEgaz
Explore posts from servers
TTCTheo's Typesafe Cult
Created by PEgaz on 11/1/2023 in #questions
problem uploading files form server action
It may be a stupid question, don't kill me, I really tried to read documentation carefully ... 🙂 Since I do some image processing at first (image is passed to backend via base64 , proccessing is done with sharp in server action), my code look like this:
const uri = fileAsBase64.split(';base64,').pop();
let buffer = Buffer.from(uri as string, 'base64');
const image = await sharp(buffer)
.resize({
width: 1140,
withoutEnlargement: true,
})
.toFormat('jpg', { compression: '80' })
.toBuffer();
// .toFile(fileName);

const file = new File(
[new Blob([image], { type: 'image/jpg' })],
'sample.jpg',
{ lastModified: Date.now(), type: 'image/jpg' }
);

const response = await utapi.uploadFiles(file);
console.log({response})
const uri = fileAsBase64.split(';base64,').pop();
let buffer = Buffer.from(uri as string, 'base64');
const image = await sharp(buffer)
.resize({
width: 1140,
withoutEnlargement: true,
})
.toFormat('jpg', { compression: '80' })
.toBuffer();
// .toFile(fileName);

const file = new File(
[new Blob([image], { type: 'image/jpg' })],
'sample.jpg',
{ lastModified: Date.now(), type: 'image/jpg' }
);

const response = await utapi.uploadFiles(file);
console.log({response})
that code keeps crushing, since File is not defined in Node What's the proper way to use newly created image (saved on server file or Buffer) and invoke on server action: await utapi.uploadFiles(...); to send files to uploadThing please help regards Piotr
2 replies