chare

chare

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.

Marked As Solved

KungPaoChicken

KungPaoChicken

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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement