如何下载文件由erlang牛仔? [英] how to download file by erlang cowboy?

查看:292
本文介绍了如何下载文件由erlang牛仔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从浏览器下载文件,我尝试通过牛仔实现,但是我失败了,浏览器告诉我从服务器接收到重复的头文件。我不知道,大家请帮助我。这是我的处理程序代码:
`

  %% @doc GET echo处理程序。 
-module(toppage_handler2)。

-export([init / 3])。
-export([handle / 2])。
-export([terminate / 3])。

init(_Transport,Req,[]) - >
{ok,Req,undefined}。

句柄(Req,State) - >
{Method,Req2} = cowboy_req:method(Req),
{Echo,Req3} = cowboy_req:qs_val(<echo>>> Req2),
{ok,Req4} = echo(Method,< Echo / binary,我在那里>>> Req3),
{ok,Req4,State}。

echo(<GET>> undefined,Req) - >
cowboy_req:reply(400,[],<Missing echo parameter。>> Req);

%%下载文件的主要部分在这里
%%我只想下载文件README.md
echo(<GET> > Echo,Req) - >
F = fun(Socket,Transport) - >
传输:sendfile(Socket,priv / README.md)
end,
Req2 = cowboy_req:set_resp_body_fun(1024,F,Req),
Req3 = cowboy_req:set_resp_header (<Content-Disposition>>GET,Req2),
Req4 = cowboy_req:set_resp_header(<attachment; filename = \README.md\ >>GET,Req3),
Req5 = cowboy_req:set_resp_header(<Content-Length>GET,Req4),
Req6 = cowboy_req:set_resp_header(<1024>GET,Req5),
cowboy_req:reply(200,[
{<content-type> ><application / octet-stream>>}
],,Req6);

echo(_,_,Req) - >
%%方法不允许。
cowboy_req:reply(405,Req)。

终止(_Reason,_Req,_State) - >
ok.`


解决方案

在处理程序中提供静态文件。
这里记载的是:



http://ninenines.eu/docs/en/cowboy/HEAD/guide/static_handlers/



有例子github:



https://github.com/ninenines/cowboy/tree/master/examples/static_world/src



这样,你不必设置标题手动,这应该可以防止错误。


I want to download file from the browser and I try to achieve by cowboy, however I failed and the browser show me that "Repeat header received from the server.". I have no idea, everyone please help me. this is my code of handler: `

%% @doc GET echo handler.
-module(toppage_handler2).

-export([init/3]).
-export([handle/2]).
-export([terminate/3]).

init(_Transport, Req, []) ->
    {ok, Req, undefined}.

handle(Req, State) ->
    {Method, Req2} = cowboy_req:method(Req),
    {Echo, Req3} = cowboy_req:qs_val(<<"echo">>, Req2),
    {ok, Req4} = echo(Method, <<Echo/binary, " I am there ">>, Req3),
    {ok, Req4, State}.

echo(<<"GET">>, undefined, Req) ->
    cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);

%% the main part of download the file is here
%% I just want to download the file README.md
echo(<<"GET">>, Echo, Req) ->
    F = fun (Socket, Transport) ->
    Transport:sendfile(Socket, "priv/README.md")
    end,
    Req2 = cowboy_req:set_resp_body_fun(1024, F, Req),
     Req3 = cowboy_req:set_resp_header(<<"Content-Disposition">>, "GET", Req2),
    Req4 = cowboy_req:set_resp_header(<<"attachment;filename=\"README.md\"">>, "GET", Req3),
     Req5 = cowboy_req:set_resp_header(<<"Content-Length">>, "GET",  Req4),
     Req6 = cowboy_req:set_resp_header(<<"1024">>, "GET",  Req5),
    cowboy_req:reply(200, [
        {<<"content-type">>, <<"application/octet-stream">>}
    ], "", Req6);

echo(_, _, Req) ->
    %% Method not allowed.
    cowboy_req:reply(405, Req).

terminate(_Reason, _Req, _State) ->
    ok.`

解决方案

Cowboy has a built in handler for serving static files. It is documented here:

http://ninenines.eu/docs/en/cowboy/HEAD/guide/static_handlers/

and there is example on github:

https://github.com/ninenines/cowboy/tree/master/examples/static_world/src

This way, you don't have to set headers manually, which should prevent the error.

这篇关于如何下载文件由erlang牛仔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆