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
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
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).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









