Skip to content

Instantly share code, notes, and snippets.

@egobrain
Created May 25, 2012 13:17
Show Gist options
  • Save egobrain/2788090 to your computer and use it in GitHub Desktop.
Save egobrain/2788090 to your computer and use it in GitHub Desktop.
upload_handler
-module(upload_handler).
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/2]).
init({_Transport, http}, Req, []) ->
{ok, Req, {}}.
handle(Req, State) ->
{Method,Req2} = cowboy_http_req:method(Req),
case Method of
'GET' ->
show_form(Req2,State);
'POST' ->
handle_upload(Req2,State)
end.
handle_upload(Req,State) ->
{ok,Req2} = loop(undefined,Req),
{ok,Req3} = cowboy_http_req:reply(200,[],<<"uploaded">>,Req2),
{ok, Req3, State}.
show_form(Req,State) ->
HTML = <<"
<html>
<form id=\"uploadForm\" action=\"/upload\" method=\"post\" enctype=\"multipart/form-data\">
<h1>Upload file</h1>
description: <input type=\"text\" id=\"video_name\" name=\"video_name\"><br>
file :<input type=\"file\" name=\"datafile\"><br>
<button class=\"btn\" type=\"submit\">Submit</button>
</form>
</html>
">>,
{ok,Req2} = cowboy_http_req:reply(200,[],HTML,Req),
{ok, Req2, State}.
terminate(_Req, _State) ->
ok.
loop(eof,Req) ->
{ok,Req};
loop(_,Req) ->
{Headers,Req2} = cowboy_http_req:multipart_data(Req),
io:format("Part: ~p",[Headers]),
loop(Headers,Req2).
-module(web_server).
-export([start/1]).
start(_Params) ->
Dispatch = [
{'_', [
{[<<"upload">>], upload_handler,[]}
]}
],
cowboy:start_listener(my_http_listener, 100,
cowboy_tcp_transport, [{port, 8080}],
cowboy_http_protocol, [{dispatch, Dispatch}]).
@Kozlov-V
Copy link

this not work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment