erlyaws / yaws

Yaws webserver
https://erlyaws.github.io
BSD 3-Clause "New" or "Revised" License
1.28k stars 267 forks source link

Post multiple files handled as one file #276

Open NoobsEnslaver opened 8 years ago

NoobsEnslaver commented 8 years ago

I use code, close to your example in yaws.pdf doc to upload files to server:

<erl>
out(A)->
    Method = yaws_api:http_request_method(A#arg.req),
    handle(Method, A).

handle('GET', A)->
    Form = {form, [{enctype, "multipart/form-data"},
           {method, post},
           {action, "my.yaws"}],
        [{input, [{type, submit}, {value, "Upload"}]},
         {input, [{type,file}, {multiple, true}, {width, "50"}, {name, "upload1"}]}]},
    {ehtml, {html,[], [{h2,[], "A simple file upload page"},Form ]}};

handle('POST', A)->
    Options = [],
    case yaws_multipart:read_multipart_form(A, Options) of
    {done, Params} ->
        io:format("~nParams: ~p", [dict:to_list(Params)]),
        {ok, [{"filename", FileName},
          {temp_file, TempFileName}, 
          {content_type, ContentType}]} = dict:find("upload1", Params),
        {ehtml, {html, [], 
             [{p, [], "File " ++ FileName ++ " was saved as " ++ TempFileName ++ ", content type: " ++ ContentType},
              {a, [{href, "my.yaws"}], "Go back"}]}};
    {error, Reason} ->
        io:format("Error reading multipart form: ~s~n", [Reason]);
    Other -> Other
    end.
</erl>

you see, i select multiple option in form, that allow me to upload multiple files. Problem, that in the end i have only one file tuple in dictionary, but i select more, and more have in /tmp folder. I think, that because dictionary store files by form name ("upload1" in my case) and second file overwrites first file tuple by same key. (P.S. sorry my English)

vinoski commented 8 years ago

Yes, this is a problem. As a workaround you can get the results for all the files if you call yaws_api:parse_multipart_post1,2 directly, but in that case you have to handle the accumulation of partial results like yaws_multipart:read_multipart_form/2 does. I'll give some thought about how to improve yaws_multipart:read_multipart_form/2 for the multiple upload case.

NoobsEnslaver commented 8 years ago

Thanks for response. Also, i would take your attention to data, returned in this example, i mean keys of fields in dictionary, that is

{ok, [{"filename", FileName},
          {temp_file, TempFileName},  
          {content_type, ContentType}]} = dict:find("upload1", Params)