-module(upload_handler).
-export([init/2]).
init(Req, Opts) ->
%{ok, Headers, Req2} = cowboy_req:part(Req),
%{ok, Data, Req3} = cowboy_req:part_body(Req2),
%{file, <<"inputfile">>, Filename, ContentType, _TE}
% = cow_multipart:form_data(Headers),
%io:format("Received file ~p of content-type ~p as follow:~n~p~n~n",
% [Filename, ContentType, Data]),
Req3 = multipart(Req),
io:format("rev...~n"),
{ok, Req3, Opts}.
file_r(_Filename) ->
case file:open( _Filename, [raw, write, binary]) of
{ok,IFile} ->
io:format("open.ok~n"),
IFile;
_Error ->
io:format("file ff~n"),
error
end.
multipart(Req) ->
case cowboy_req:part(Req) of
{ok, Headers, Req2} ->
Req4 = case cow_multipart:form_data(Headers) of
{data, _FieldName} ->
{ok, _Body, Req3} = cowboy_req:part_body(Req2),
Req3;
{file, _FieldName, _Filename, _CType, _CTransferEncoding} ->
io:format("filename:~p~n",[_Filename]),
Iff = file_r( _Filename ),
io:format("fdsfs:~p~n",[Iff]),
stream_file(Req2,Iff),
file:close(Iff)
end,
multipart(Req4);
{done, Req2} ->
Req2
end.
stream_file(Req,Iff) ->
case cowboy_req:part_body(Req) of
{ok, _Body, Req2} ->
file:write(Iff, _Body),
Req2;
{more, _Body, Req2} ->
file:write(Iff, _Body),
stream_file(Req2,Iff)
end.
-export([init/2]).
init(Req, Opts) ->
%{ok, Headers, Req2} = cowboy_req:part(Req),
%{ok, Data, Req3} = cowboy_req:part_body(Req2),
%{file, <<"inputfile">>, Filename, ContentType, _TE}
% = cow_multipart:form_data(Headers),
%io:format("Received file ~p of content-type ~p as follow:~n~p~n~n",
% [Filename, ContentType, Data]),
Req3 = multipart(Req),
io:format("rev...~n"),
{ok, Req3, Opts}.
file_r(_Filename) ->
case file:open( _Filename, [raw, write, binary]) of
{ok,IFile} ->
io:format("open.ok~n"),
IFile;
_Error ->
io:format("file ff~n"),
error
end.
multipart(Req) ->
case cowboy_req:part(Req) of
{ok, Headers, Req2} ->
Req4 = case cow_multipart:form_data(Headers) of
{data, _FieldName} ->
{ok, _Body, Req3} = cowboy_req:part_body(Req2),
Req3;
{file, _FieldName, _Filename, _CType, _CTransferEncoding} ->
io:format("filename:~p~n",[_Filename]),
Iff = file_r( _Filename ),
io:format("fdsfs:~p~n",[Iff]),
stream_file(Req2,Iff),
file:close(Iff)
end,
multipart(Req4);
{done, Req2} ->
Req2
end.
stream_file(Req,Iff) ->
case cowboy_req:part_body(Req) of
{ok, _Body, Req2} ->
file:write(Iff, _Body),
Req2;
{more, _Body, Req2} ->
file:write(Iff, _Body),
stream_file(Req2,Iff)
end.