user6574649
user6574649

Reputation:

Uploading and accessing files in Phoenix and storing them locally

I want to be able to upload files, and in particular images, and store them locally. I'm aware of plug Plug.Static, but isn't it for static assets? Whereas uploaded files aren't static.

1) Also, I already have this in my application generated automatically:

# lib/my_app/endpoint.ex
plug Plug.Static,
  at: "/", from: :my_app, gzip: false,
  only: ~w(css fonts images js favicon.ico robots.txt)

Indeed, it's for static assets. How should I go about uploaded files? I want to store them in a different directory. Not necessarily outside of my project, but anywhere where it won't be compiled in the production beam file by default.

2) Given an uploaded file name, I want to be able to get its full url/path from a) controller b) view. How can I do that?

Upvotes: 0

Views: 1560

Answers (1)

Alex Garibay
Alex Garibay

Reputation: 391

Check out the docs for file uploads. Phoenix uses Plug.Upload. Your files will be uploaded to a temporary directory and only exists during the lifecycle of the upload request. You can choose to do whatever you want with that file. If you want to store it somewhere on your server, you might want to keep track of what files you upload and where they are stored on your server in your database.

Upvotes: 1

Related Questions