How do you post a file to a custom api endpoint?

I’m writing a plugin where I get data from a database via an api endpoint and display it in a panel view. That part is working fine. But now I want to add a file upload on the same panel view where you can update the database from a csv file.

My reasoning was: I write a component with a file input, and on submit post it to a custom api route where I parse the csv and update the database.

But I can’t figure out how to post the csv to my api endpoint using Kirby stuff. From what I gather, this.$api.post() doesn’t allow you to post a file and when I use the <k-upload> component, the upload is successful but the response is undefined, even if my endpoint just returns some json so I have no idea what happens there.

I got it to work with a plain XMLHttpRequest(), but it leaves me wondering if there’s an easier/cleaner approach?

I just added a new articles to our docs how to handle uploads in the panel: https://getkirby.com/docs/reference/plugins/internals/uploads I hope it helps!

1 Like

Awesome, will check it out.

Rewrote my custom component & endpoint following the guide and it’s all working perfectly. Thanks!

1 Like