Download files using Vapor

I saw in the documentation in the body section that there is support for downloading files right now - or at least I understand it this way 😅

I don't have a solid foundation for backend development, especially when it comes to wireframes that are still growing as fast as Vapor. I wonder if anyone can provide an example of a real file upload? I was hoping for a simple web page with the ability to upload the file to the backend and then save it for future use.

+8
vapor
source share
1 answer

Vapor allows you to upload files using Multipart encoding. You can learn more about downloading HTTP here:

How does uploading an HTTP file work?

And also here:

What does enctype = 'multipart / form-data' mean?

So, the HTML code for uploading a file to Vapor will look something like this:

<form action="upload" method="POST" enctype="multipart/form-data"> <input type="text" name="name"> <input type="file" name="image"> <input type="submit" value="Submit"> </form> 

And then the code in Vapor

 drop.get("form") { req in return try drop.view("form.html") } drop.post("upload") { req in let name = req.data["name"] let image = req.data["image"] // or req.multipart["image"] ... } 

In terms of image storage, it is up to you. You can save to the database or create a folder in the system to which you have write access.

+18
source share

All Articles