Skip to content

Instantly share code, notes, and snippets.

@221V

221V/hm.erl Secret

Created November 2, 2018 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 221V/f80fc360040143d5360e5c5dcfb751d6 to your computer and use it in GitHub Desktop.
Save 221V/f80fc360040143d5360e5c5dcfb751d6 to your computer and use it in GitHub Desktop.
n2o websoket file upload example
...
filename(#ftp{sid=_Sid,meta=Meta,other1=Other1,other2=Other2,other3=Other3,filename=FileName}) ->
io:format("filename callback Other1, Other2, Other3: ~p ~p ~ts ~n",[Other1, Other2, Other3]),
%filename:join(wf:to_list(Sid),FileName).
PathName = ["img/", ?MODULE:date2list(calendar:local_time()), "/", Meta],
filename:join(PathName,FileName).
date2list({{Year,Month,Day},_}) ->
[erlang:integer_to_list(Year), "/", ?MODULE:make_valid_day_list(Month), "/", ?MODULE:make_valid_day_list(Day) ].
...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{pagetitle}}</title>
</head>
<body>
<script>{{script}}</script>
<script type="text/javascript" src="/js/n2o/protocols/bert.js" defer></script>
<script type="text/javascript" src="/js/n2o/protocols/client.js" defer></script>
<script type="text/javascript" src="/js/n2o/protocols/nitrogen.js" defer></script>
<script type="text/javascript" src="/js/n2o/bullet.js" defer></script>
<script type="text/javascript" src="/js/n2o/xhr.js" defer></script>
<script type="text/javascript" src="/js/n2o/utf8.js" defer></script>
<script type="text/javascript" src="/js/n2o/template.js" defer></script>
<script type="text/javascript" src="/js/n2o/n2o.js" defer></script>
<script type="text/javascript" src="/js/n2o/validation.js" defer></script>
<script>
window.addEventListener("load", function load(e0){
protos = [$client,$bert]; N2O_start();
},false);
</script>
</body>
</html>
...
route(<<"test_upload/">>) -> test_upload;
...
...
{upload,"/var/www/test/uploads/"},
{filename,hm},
...
-module(test_upload).
-compile([export_all, nowarn_export_all]).
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
main() ->
#dtl{file = "null", app=test,bindings=[ {pagetitle, <<"test file upload">>} ]}.
event(init) ->
wf:reg(n2o_session:session_id()),
%io:format(">>> ~p~n",[n2o_session:session_id()]),
wf:wire("var cont = document.body;cont.insertAdjacentHTML('afterBegin', '<div id=\"test_upload\"></div><br><span id=\"ftp_status\"></span>');"
"var load_js=document.createElement('script');load_js.setAttribute('defer','defer');load_js.setAttribute('src', '/js/n2o/ftp.js');document.body.appendChild(load_js);"),
wf:wire(wf:f("ftp_file=undefined;function bind_start_upload(x,meta){x.addEventListener('change',function(e){ftp.meta = utf8_toByteArray(meta);ftp.other1 = bin('777');ftp.other3 = utf8_toByteArray('~s');ftp.autostart = true;ftp.init(this.files[0]);});};"
"qi('test_upload').innerHTML='~s';"
"bind_start_upload(qi('~s'),'~s');",[
unicode:characters_to_binary("тест", utf8),
%"test",
<<"<label><input id=\"upload_form_1\" type=\"file\"><span>Browse &amp; Upload</span></label>">>,<<"upload_form_1">>,<<"upload_form_1">>]));
event(#ftp{sid=Sid,filename=Filename,other1=Other1,other2=Other2,other3=Other3,status={event,init}}=Data) ->
%wf:wire("alert('start send');"),
io:format("test_upload: ~p~n",["start send"]),
io:format("start upload Other1, Other2, Other3: ~p ~p ~ts ~n",[Other1, Other2, unicode:characters_to_binary(Other3, utf8, latin1)]),
ok;
event(#ftp{sid=Sid,filename=Filename,other1=Other1,other2=Other2,other3=Other3,status={event,stop}}=Data) ->
wf:wire("alert('sended');"),
io:format("test_upload: ~p~n",["sended"]),
io:format("start upload Other1, Other2, Other3: ~p ~p ~ts ~n",[Other1, Other2, Other3]),
ok;
event(Event) ->
wf:info(?MODULE,"Event: ~p", [Event]),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment