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?
Can you not send the file in the body of your post request to the function?
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.
I recommend using the storage endpoint with the SDK, since if not you will need to chunk manually big files
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
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
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.
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
Could you use something like this to parse the formdata https://www.npmjs.com/package/multiparty
i'm not sure how it'll help. afaik, appwrite server function have a specific syntax that we need to follow
Appwrite functions don't handle binary data well at the moment
any workaround for my scenario?
I highly suggest using our storage service
i will use the storage service. but in the server function. otherwise, i cannot set permission for the files properly
i tried sending the formdata as the body, ignoring the TS error. but it thrown "body" param can only be string
error
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.
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?
Sure ya
[SOLVED] can i send files to a server function?
Recommended threads
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...
- Function in Node.JS to monitor events ar...
Hello everyone. I'm creating my first Node.JS function, but I don't have much experience with node and javascript. I'm trying to create a function, that monito...
- Deploy function not working - 503
Hellon i get this error message, when i try to deploy a new version of a function <html><body><h1>503 Service Unavailable</h1>No server is available to handle...