adrian

adrian

Storing and uploading files using the JSON API?

Hello,

I couldn’t find how to deal with files using ash in the docs.

I found the File Type but it’s not what I expect.

I want to be able to store the file on disk and save the path in the database.

In the future I want to upload files using the JSON API.

Thank you!

Most Liked

zachdaniel

zachdaniel

Creator of Ash

The :file type is primarily meant for accepting files over interfaces. You’d add a file as an argument, and then use a change on the action to take that file and do something with it. Like you can move it from the temporary directory it is uploaded to by default when it’s a plug.upload, etc.

Really need to write a more comprehensive guide on dealing with file uploads: AshJsonApi.Plug.Parser — ash_json_api v1.7.0

zachdaniel

zachdaniel

Creator of Ash

Correct on the admin question, but not yet, but we would like to implement that :slight_smile:

You can potentially use Waffle, although not something like WaffleEcto

create :create_user do
  argument :profile_photo, :file
  change fn changeset, _ ->
    Ash.Changeset.before_transaction(changeset, fn changeset, _ ->
      if upload = changeset.arguments[:file] do
         url = <upload to s3>
         Ash.Changeset.force_change_attribute(changeset, :profile_photo_url, url)
      else
         changeset
      end
    end
  end
end
zachdaniel

zachdaniel

Creator of Ash

It would be a welcome addition :slight_smile: I suspect that it will be surprisingly complex but I could be wrong. Ultimately the pattern would be to fork ash_admin and then submit a PR. Everything would be done in the form component when rendering attribute inputs.

Last Post!

artyom87

artyom87

Yeah, right. Images and files are definitely a big problem. Although images and files are an integral part of any web project. Fork ash_admin - is just not right. There is still a lack of taxonomy in the ash_admin data tables.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement