How to allow user to download a file in ChicagoBos

2019-02-28 22:45发布

问题:

I generate one xml and store it to some location say myproject/media/doc.xml As I'm newbie developer with erlang. What I knew is I will have to set request headers like

"Content-Disposition", "attachment; filename=\"" + Filename + "\""

But I'm not sure how to achieve this with ChicagoBoss. I followed the links:

  1. How to download files in ChicagoBoss?
  2. how to download file by erlang cowboy?

But still I did not get the clear idea about how to do that. What we tried to do is:

download_xml('GET', [])->
    Fun = fun (Socket, Transport) ->
      Transport:sendfile(Socket, "/home/laxmikant/Desktop/demo.xml")
    end,
    Req2 = cowboy_req:set_resp_body_fun( F, Req),%% Req is available here
    Req3 = cowboy_req:set_resp_header(<<"Content-Disposition">>, "GET", Req2),
    Req4 = cowboy_req:set_resp_header(<<"attachment;filename=\"demo.xml\"">>, "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).

But seems to be ChicagoBoss Request object doesn't match the Cowboy Req.