如何将多个文件上传到牛仔
How to upload multiple files into cowboy
正在尝试使用 curl 上传文件
curl -v -F 'file1=@/home/user/test/aaa/validation_utils.erl' -F 'file2=@/home/user/test/aaa/README.md' http://localhost:8083/api/list
但只收到一个(从日志中可以看出)
[warning] Form_Data:
[{<<"content-type">>,<<"application/octet-stream">>},{<<"content-disposition">>,<<"form-data; name=\"file1\"; filename=\"validation_utils.erl\"">>}]
这是我的 get_body 功能:
get_body(<<"POST">>, AllHeaders, Req, _MaxFileSize) ->
case maps:get(<<"content-type">>, AllHeaders) of
<<"application/json", _Rest/binary>> ->
case catch cowboy_req:read_body(Req) of
{ok, <<>>, Req2} ->
{ok, [], Req2};
{ok, JsonBody, Req2} when JsonBody =/= <<>> ->
try
{ok, ?JSON_DECODE(JsonBody), Req2}
catch
_:Err ->
Err
end
end;
<<"multipart/form-data", _Rest/binary>> ->
{ok, Headers, Req2} = cowboy_req:read_part(Req),
{ok, Data, _Req3} = cowboy_req:read_part_body(Req2),
{file, _, Filename, _ContentType, _BitSize} = cow_multipart:form_data(Headers),
Body = [{<<"filename">>, Filename}, {<<"base64_file">>, base64:encode(Data)} | cowboy_req:parse_qs(Req)],
{ok, Body, Req};
_ ->
cowboy_req:read_urlencoded_body(Req)
end;
知道了。
我需要迭代 cowboy_req:read_part(Req)
multipart(Req0) ->
case cowboy_req:read_part(Req0) of
{ok, Headers, Req1} ->
?LOG_WARNING("Headers: p~n", [Headers]),
{ok, _Body, Req} = cowboy_req:read_part_body(Req1),
multipart(Req);
{done, Req} ->
?LOG_WARNING("Req: p~n", [Req]),
Req
end.
如此处所述:https://ninenines.eu/docs/en/cowboy/2.0/guide/multipart/
正在尝试使用 curl 上传文件
curl -v -F 'file1=@/home/user/test/aaa/validation_utils.erl' -F 'file2=@/home/user/test/aaa/README.md' http://localhost:8083/api/list
但只收到一个(从日志中可以看出)
[warning] Form_Data:
[{<<"content-type">>,<<"application/octet-stream">>},{<<"content-disposition">>,<<"form-data; name=\"file1\"; filename=\"validation_utils.erl\"">>}]
这是我的 get_body 功能:
get_body(<<"POST">>, AllHeaders, Req, _MaxFileSize) ->
case maps:get(<<"content-type">>, AllHeaders) of
<<"application/json", _Rest/binary>> ->
case catch cowboy_req:read_body(Req) of
{ok, <<>>, Req2} ->
{ok, [], Req2};
{ok, JsonBody, Req2} when JsonBody =/= <<>> ->
try
{ok, ?JSON_DECODE(JsonBody), Req2}
catch
_:Err ->
Err
end
end;
<<"multipart/form-data", _Rest/binary>> ->
{ok, Headers, Req2} = cowboy_req:read_part(Req),
{ok, Data, _Req3} = cowboy_req:read_part_body(Req2),
{file, _, Filename, _ContentType, _BitSize} = cow_multipart:form_data(Headers),
Body = [{<<"filename">>, Filename}, {<<"base64_file">>, base64:encode(Data)} | cowboy_req:parse_qs(Req)],
{ok, Body, Req};
_ ->
cowboy_req:read_urlencoded_body(Req)
end;
知道了。
我需要迭代 cowboy_req:read_part(Req)
multipart(Req0) ->
case cowboy_req:read_part(Req0) of
{ok, Headers, Req1} ->
?LOG_WARNING("Headers: p~n", [Headers]),
{ok, _Body, Req} = cowboy_req:read_part_body(Req1),
multipart(Req);
{done, Req} ->
?LOG_WARNING("Req: p~n", [Req]),
Req
end.
如此处所述:https://ninenines.eu/docs/en/cowboy/2.0/guide/multipart/