我在网址提示下引用。
Elixir/Phoenix file upload folder
我有同样的问题。我想知道为什么我不能将上传的文件移动到dest文件夹。 在绝对路径的情况下,它的工作原理。 如何设置相对路径设置?
upload_controller.ex
defmodule MyAppWeb.UploadController do
use MyAppWeb, :controller
def create(conn, %{"user" => user_params}) do
IO.inspect user_params
if upload = user_params["photo"] do
extension = Path.extname(upload.filename)
# It worked.
# File.cp(upload.path, "/Users/myaccount/MyApp/priv/static/media/profile#{extension}")
# It didn't work.
case File.cp(upload.path, "media/profile#{extension}") do
{:ok, _result} ->
conn
|> put_flash(:info, "File Uploaded successfully.")
|> redirect(to: upload_path(conn, :index))
{:error, _result} ->
IO.inspect(_result)
conn
|> put_flash(:info, "File Uploaded failed!!.")
|> redirect(to: upload_path(conn, :index))
end
end
end
endpoint.ex
plug Plug.Static,
at: "/", from: :fish_pot, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)
#Added
plug Plug.Static, at: "/uploads", from: "media/"
我想在priv/static/media
上传文件。 IO.inspect返回:enoent
。