Uploading files using Vapor

2020-08-22 03:33发布

I've seen in documentation in Body section that there's a support for file uploading right now - or at least I understand it this way

标签: vapor
1条回答
做自己的国王
2楼-- · 2020-08-22 03:46

Vapor allows for file upload using the Multipart encoding. You can read more about HTTP upload here:

How does HTTP file upload work?

And also here:

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

So the HTML code to upload a file to Vapor would look something like:

<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 how to store the image, that is up to you. You can store in a database or create a folder on the system to which you have write access.

查看更多
登录 后发表回答