Phoenix 1.7 File Upload and Download

I’m trying to figure out how to do file download and upload in phoenix for the first time using Phoenix 1.7.1. Is there a guide on how to do this that I’ve someone not found? Say that I want a resource for books with no liveview. With the following command I get urls for creating, editing, viewing books.
mix phx.gen.html Books Book books title:string author:string

But what if I want the forms to have in addition to title and author an upload for a pdf where it is stored in the database as a binary blob alongside the title and author. What is the idiomatic way I’m suppose to do that in Phoenix. Also what is the idiomatic url route I’m suppose to use to expose downloading the file? I’ve never done this before so I don’t know what would be the restful conventions in Phoenix for this.

When a file input is submitted, information about it it put in a Plug.Upload struct that can be accessed from the params in the controller. There was an official blog post explaining how it works, the code examples should work with minor changes.

From there you can copy the temporary file to the filesystem or S3 or somewhere else, and save the path as a string in the database. I remember saving binary blobs into the database was not recommended for performance reasons, but databases have changed a lot and I am not up-to-date with current features/best practices.

Downloads can be done with send_download/3 from the controller. A route like get "/books/:id/download" should be alright.

Don’t know where you are hosting your application but arc and ex_aws are two useful libraries to look at if you are using s3

That blog post was helpful. The following worked for me.

<.simple_form :let={f} for={@changeset} action={@action} multipart>
  ...
  <%= Phoenix.HTML.Form.file_input(f, :photo) %>

And yah binary blobs I’ve heard aren’t advised to put into databases but I don’t want to deal with how to keep backups of the database and filesystem in sync.