benonymus

benonymus

Questions about ARC in phoenix/elixir api

Hey I am trying to save a profile picture from my react native app with phoenix api.
So I started to use arc, following this guide: https://medium.com/@rafamossakowski/arc-carrierwave-for-phoenix-1064b2463fdf since I couldn’t find one that is using react native. It is working nicely with the website part, but my questions would be that:
How to send the picture form my app to the api in json?
And how to return the picture to the app from the api in json?

Thanks

First Post!

kokolegorille

kokolegorille

I usually base64 encode the image. The result is just a string. There is a little overhead due to encoding.

On the server side, I use something like this to convert the base64 string to file.

  # decode base64 string to binary
  defp sanitize_params(%{"base64" => base64} = params) do
    params
    |> Map.put("document_source", Base.decode64!(base64))
    |> Map.delete("base64")
  end

The sidenote is I needed to update endpoint like this, to increase the allowed size. This was needed when sending hires pictures from Iphone to backend.

  plug(
    Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Poison,
    #
    # Increase the size for file uploads
    # https://chodounsky.net/2015/06/05/increase-request-length-in-phoenix-to-upload-large-files/
    # https://hexdocs.pm/plug/Plug.Parsers.html
    #
    length: 100_000_000
  )

PS. This is old code… still using Poison instead of Jason…

On the frontend (React Native) I just prepare data like this before sending to backend.

    const body = {
      document_type: documentType,
      base64: documentSource.base64,
    }

Most Liked

stuartish

stuartish

I’m sure this is too little too late, but in case anyone else has the same question, I think the best thing to do, if you need to save that file to S3 or a database, is to turn it into an upload plug. (Stole this from somewhere I can’t remember)

  def get_plug(data) do
    {:ok, binary} = Base.decode64(data)

    with {:ok, path} <- Plug.Upload.random_file("upload"),
         {:ok, file} <- File.open(path, [:write, :binary]),
         :ok <- IO.binwrite(file, binary),
         :ok <- File.close(file) do
      {:ok, %Plug.Upload{path: path}}
    end
  end

That returns the struct you would have gotten had you uploaded a file directly via form-data (though it will be missing mimetype and filename).

Where Next?

Popular in Questions Top

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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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

Other popular topics Top

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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement