Skip to content

Instantly share code, notes, and snippets.

@CharlesOkwuagwu
Last active February 23, 2016 16:09
Show Gist options
  • Save CharlesOkwuagwu/543b637b9881f94531ff to your computer and use it in GitHub Desktop.
Save CharlesOkwuagwu/543b637b9881f94531ff to your computer and use it in GitHub Desktop.
Error trying file upload with Sugar-framework and Plug.Upload
Erlang/OTP 18 [erts-7.2.1] [64-bit] [smp:8:8] [async-threads:10]
Compiled lib/simple/controllers/main.ex
Generated index.html.eex
Generated show.html.eex
Generated index.html.eex
Interactive Elixir (1.3.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
16:54:44.422 [error] Ranch listener Simple.Router.HTTP had connection process started with :cowboy_protocol:start_link/4 at #PID<0.451.0> exit with reason: {{%Plug.Parsers.ParseError{exception: %RuntimeError{message: "could not find process Plug.Upload. Have you started the :plug application?"}, plug_status: 400}, [{Plug.Parsers.MULTIPART, :parse, 5, [file: 'lib/plug/parsers/multipart.ex', line: 14]}, {Plug.Parsers, :reduce, 6, [file: 'lib/plug/parsers.ex', line: 186]}, {Simple.Router, :do_call, 2, [file: 'lib/simple/router.ex', line: 1]}, {Plug.Adapters.Cowboy.Handler, :upgrade, 4, [file: 'lib/plug/adapters/cowboy/handler.ex', line: 15]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 442]}]}, {Simple.Router, :call, [%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [], body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "POST", owner: #PID<0.451.0>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: ["file", "upload"], peer: {{127, 0, 0, 1}, 3319}, port: 4000, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"host", "localhost:4000"}, {"connection", "keep-alive"}, {"content-length", "47237"}, {"cache-control", "max-age=0"}, {"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}, {"origin", "http://localhost:4000"}, {"upgrade-insecure-requests", "1"}, {"user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2657.0 Safari/537.36"}, {"content-type", "multipart/form-data; boundary=----WebKitFormBoundarycxFLRuZhMurzdnLo"}, {"dnt", "1"}, {"referer", "http://localhost:4000/"}, {"accept-encoding", "gzip, deflate"}, {"accept-language", "en-US,en;q=0.8,ms;q=0.6,ig;q=0.4"}, {"cookie", "ASP.NET_SessionId=dm0jgukswqmzdvwvlgf432bh"}], request_path: "/file/upload", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: nil, state: :unset, status: nil}, []]}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Hosted Services</title>
<!-- Bootstrap -->
<link href="static/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="static/js/html5shiv.min.js"></script>
<script src="static/js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Welcome Home!</h1>
<form enctype="multipart/form-data" action='/file/upload' method='post'>
<input type='file' name='image'>
<input type='submit'>
</form>
<script src="static/js/jquery-1.12.1.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
</body>
</html>
defmodule Simple.Controllers.Main do
use Sugar.Controller
def index(conn, []) do
raw conn |> render #resp(200, "<h1>Hello guys</h1>")
end
def file_upload(conn, _params) do
IO.puts "#{inspect conn}"
case conn.params["image"] do
%Plug.Upload{filename: filename, path: path} ->
{:ok, image} = File.read path
{:ok, file} = File.open filename, [:write]
IO.binwrite file, image
File.close file
conn |> resp(200, "image[" <> filename <> "] was saved!")
nil ->
conn |> resp(400, "image was not attached.")
end
end
end
defmodule Simple.Router do
use Sugar.Router
plug Sugar.Plugs.HotCodeReload
if Sugar.Config.get(:sugar, :show_debugger, false) do
use Plug.Debugger, otp_app: :simple
end
plug Plug.Static, at: "/static", from: :simple
# Uncomment the following line for session store
plug Plug.Session, store: :ets, key: "sid", secure: true, table: :session
# Define your routes here
get "/", Simple.Controllers.Main, :index
get "/file/upload", Simple.Controllers.Main, :file_upload
end
Erlang/OTP 18 [erts-7.2.1] [64-bit] [smp:8:8] [async-threads:10]
Generated index.html.eex
Generated show.html.eex
Generated index.html.eex
Interactive Elixir (1.3.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
17:03:11.194 [error] #PID<0.439.0> running Simple.Router terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
** (Plug.Conn.AlreadySentError) the response was already sent
(plug) lib/plug/conn.ex:343: Plug.Conn.send_resp/1
(simple) lib/simple/controllers/main.ex:1: Simple.Controllers.Main.call/2
(simple) lib/simple/router.ex:1: Simple.Router.do_call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment