I am trying to implement support for uploading a user avatar in my Phoenix application. I have tried two ways and run into different issues.
Upload the files into the priv/static folder. This works and the files can be retrieved using the Routes.static_path(…). Problem(and this took me a while to figure out what was going on): Each time a file is uploaded, live reload kicks in and the browser reloads the page and is not following the redirect that the controller is doing. Obviously this is only and issue with dev env with the live reload. Is there a way to have live reload ignore a folder inside priv/static?
When I try and save the uploaded files into an arbitrary directory outside the priv folder, I can’t figure out how to access the file to download it. Is there a way to access directory inside the phoenix project that is outside the priv folder?
Or perhaps I am going about this the hard way and there is a better way.
You could save into priv/static/something/ and exclude this path from live reload.
You configure this in config/dev.exs
You can save wherever You want… and configure plug static to serve this wherever.
You can also save file anywhere, and have them served by a controller.
Is there a way to exclude a directory explicitly from live reload? currently it reloads all files in the priv/static folder. So if I have a folder under priv/static/uploads, can I specifically tell the config to ignore the upload folder, but include all other folders? I suppose I could just list the sub-directories in priv/static explicitly instead of using the wildcard priv/static/*
The issue I have with serving the files from directory is how to build the path in the controller. It seems like when I run the application in dev env, the files are rooted in _build folder, so I am not sure how I can access files in an arbitrary file path in the project.
I do something like this in the controller…
|> send_file(200, path)
You can also use send_download.
The live reload patterns are in config/dev.exs
I think it’s possible to exclude, but don’t remember the syntax.
Yes, Thanks - i was able to get both methods working.
- I did not find a solution to exclude a folder from live-reload, but a simple pattern includes the files that I wanted:
- My filename had a suffix so it was not able to find the file.
path = String.replace(path, ~r/\?v.*$/, "")
. Once I removed the suffix , it worked.