Back

[SOLVED] can i send files to a server function?

  • 0
  • Functions
  • Web
manazo
15 Nov, 2023, 19:07

I'm working on a chat application. and my whole "send message" functionality is handled in a server function and I'm just calling that function from my app with the chat token and the message content. till now my app only had the feature to send text messages. but now I'm thinking of implementing a "send attachment" feature as well. which means, I need to send those files to my server functions to work with them.

u might suggest uploading the files from my app and then only sending the file links to the server function. but in that case, I can't set the Read Permission of that file for the other user (message recipient). cause we cannot set permissions that we don't have, and that's the same reason why I had to move my whole "send message" functionality to a server function. to set Read Permissions for both users (message sender and recipient)

So is there any way I can send files to my server function? or any workaround u can think of for my scenario?

TL;DR
You can send files to a server function by uploading them from the client-side with sender read permission, then sending the file ID to the server function and updating the file permissions. You can trigger the function on file creation or have the user execute the function. Use the storage service in the server function to set the file permissions properly. Another option is to set headers to multipart/form-data and convert the FormData object to a string before sending it to the server function. You can also use the storage endpoint with the SDK to avoid manual chunking of large files.
Kenny
15 Nov, 2023, 19:42

Can you not send the file in the body of your post request to the function?

Kenny
15 Nov, 2023, 19:46

If using javascript you can create a formData object and append the image, aswell as appending your chat token and message content. You would then set the POST type to multipart/form-data. Once you've got it on your function you'd then decode the form data and do whatever you need with it.

D5
15 Nov, 2023, 20:01

I recommend using the storage endpoint with the SDK, since if not you will need to chunk manually big files

manazo
15 Nov, 2023, 21:04

the body parameter in the createExecution method accepts only string type data. so if i try to pass a FormData type value, it'll at least show a TS error. not sure if the data will actually be passed (can't test now. appwrite server seems to be down atm) and even if the data gets passed, how can i retrieve any formdata in the server function? i can't find anything in the doc regarding how to access formdata from the req param. it only shows ways to access JSON type data. and also, there's no way for me to log the whole req object, cuz the log function only shows string type data in the console

manazo
15 Nov, 2023, 21:05

yeah i'll do that (tho each of my files is not that big to be chunked. size limit is 10MB for each). but first, i need to send those files to the server function successfully

Kenny
15 Nov, 2023, 21:10

Hmm, can you set headers to have type multipart/form-data, set the method to post, and convert the formdata object to string? I've not tried that so I'm not really sure. You always have the option of making a fetch request to the function url too.

manazo
15 Nov, 2023, 21:16

the createExecution method does have a headers parameter that i can pass. but the qs of how I'll retrieve the formdata on the server function still stands. no matter if i create the execution with function API or manually by a fetch request

Kenny
15 Nov, 2023, 21:26

Could you use something like this to parse the formdata https://www.npmjs.com/package/multiparty

manazo
15 Nov, 2023, 21:34

i'm not sure how it'll help. afaik, appwrite server function have a specific syntax that we need to follow

Drake
15 Nov, 2023, 21:41

Appwrite functions don't handle binary data well at the moment

manazo
15 Nov, 2023, 22:46

any workaround for my scenario?

Drake
16 Nov, 2023, 00:31

I highly suggest using our storage service

manazo
16 Nov, 2023, 07:07

i will use the storage service. but in the server function. otherwise, i cannot set permission for the files properly

manazo
16 Nov, 2023, 16:55

i tried sending the formdata as the body, ignoring the TS error. but it thrown "body" param can only be string error

Drake
16 Nov, 2023, 18:21

you can have the user execute the function passing in the file id. or you can trigger the function on file create...and then you can check the headers...the user might be listed there.

manazo
16 Nov, 2023, 18:38

u mean first uploading the files from the client-side with only the sender read permission. then send the file id to the server function and update the permission of those files in the server function?

Drake
16 Nov, 2023, 20:41

Sure ya

manazo
17 Nov, 2023, 06:58

[SOLVED] can i send files to a server function?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more