You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ectophile is an extension for Ecto models to instantly support file uploads.
Usage
Ectophile provides an attachment_fields/2 macro for your model which is used like:
defmoduleMyApp.UserdouseEctophile#=> Note that this needs to be used before MyApp.Web so that the callbacks will workuseMyApp.Web,:modelschema"users"dofield:email,:stringattachment_fields:avatartimestampsend...end
attachment_fields/2 in the above example, defines two different fields which are:
field :avatar, Ectophile.Type
field :avatar_upload, :any, virtual: true
The :avatar field is where the path to the file in your filesystem and filename is saved. :avatar_upload is the field we'll use for file uploads.
Keep in mind that you will need to create the necessary migration to add the Ectophile fields to your model like so:
defmoduleSampleMigrationdouseEcto.Migrationdefchangedocreatetable(:users)doadd:emailadd:avatar,:jsonb#=> The column we need for Ectophile's custom fieldtimestampsendendend
In your application's top-level supervisor's start/2 function, add the following to setup the directories where your files will be uploaded:
importEctophile.Helpersdefstart(_type,_args)doensure_upload_paths_exist([MyApp.User])#=> This creates all the required directories for your uploaded files...end
After doing the migrations and defining your model's attachment_fields, you can then add file upload field to your model's form like:
That's it!!! Now every time a user uploads a file and submits a form, that file is stored in a configurable location in your priv/static directory and a reference to that file is stored in your database.